将数组格式的模型传递给 YII 中的视图

发布于 2024-09-11 11:05:55 字数 1532 浏览 4 评论 0原文

如何以数组格式传递模型。 我想将这种格式的模型从控制器传递到视图:-

Users[user_contact]=Contact 用户[user_contact][contat_city]=城市 Users[user_contact][contact_state]=state

这就是我正在做的

public function actionCreate() {
    $user = new Users;
    $presContact = new Contacts;
    $presCity = new Cities;
    $presState = new States;
    $contactArr = array();
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if (isset($_POST['Users'])) {
        $transaction = CActiveRecord::getDBConnection()->beginTransaction();
        $contactArr = CommonFunctions::saveContact($_POST['Users']['user_pres_contact'],'user_pres_contact',$errorArr);
        $presContact = $contactArr['contact'];
        $presCity = $contactArr['city'];
        $presState = $contactArr['state'];
        $user->attributes = $_POST['Users'];
        $user->user_pres_contact_id = $presContact->contact_id;
        if($user->save()){
            $transaction->commit();
            $this->redirect(array('view', 'id' => $user->user_id));
        } else {
            $transaction->rollback();

        }
    }

    $this->render('createUser', array(
        'Users' => $user,
        'Users[\'user_pres_contact\']'=>$presContact,
        'Users[\'user_pres_contact\'][\'contact_city\']'=>$presCity,
        'Users[\'user_pres_contact\'][\'contact_state\']'=>$presState,
    ));
}

我只能访问 $users 但是 我无法在视图中访问 $Users['user_pres_contact']

How can I pass the model in array format.
I want to pass models in this format from controller to view:-

Users[user_contact]=Contact
Users[user_contact][contat_city]=City
Users[user_contact][contact_state]=state

This is what I am doing

public function actionCreate() {
    $user = new Users;
    $presContact = new Contacts;
    $presCity = new Cities;
    $presState = new States;
    $contactArr = array();
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if (isset($_POST['Users'])) {
        $transaction = CActiveRecord::getDBConnection()->beginTransaction();
        $contactArr = CommonFunctions::saveContact($_POST['Users']['user_pres_contact'],'user_pres_contact',$errorArr);
        $presContact = $contactArr['contact'];
        $presCity = $contactArr['city'];
        $presState = $contactArr['state'];
        $user->attributes = $_POST['Users'];
        $user->user_pres_contact_id = $presContact->contact_id;
        if($user->save()){
            $transaction->commit();
            $this->redirect(array('view', 'id' => $user->user_id));
        } else {
            $transaction->rollback();

        }
    }

    $this->render('createUser', array(
        'Users' => $user,
        'Users[\'user_pres_contact\']'=>$presContact,
        'Users[\'user_pres_contact\'][\'contact_city\']'=>$presCity,
        'Users[\'user_pres_contact\'][\'contact_state\']'=>$presState,
    ));
}

I am able to access only $users but
I m not able to access $Users['user_pres_contact'] in the view

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2024-09-18 11:05:56

那是因为你将它们分配为字符串......
正确的做事方法是(顺便说一句,你所要求的不能从字面上完成,不可能为一个键分配2个值):

$user = array(
'user_press_contact' => array(
  'contact' => $presContact,
  'city' => $presCity,
  'state' => $presState,
 ),
);


$this->render('createUser', array(
        'Users' => $user,
    ));

它会给你 $Users['user_press_contact']['contact']视图中的名称等

That's because you are assigning them as strings...
The correct way of doing things would be (btw, what you are asking for can't done literally, it is impossible to assign 2 values to one key):

$user = array(
'user_press_contact' => array(
  'contact' => $presContact,
  'city' => $presCity,
  'state' => $presState,
 ),
);


$this->render('createUser', array(
        'Users' => $user,
    ));

It will give you $Users['user_press_contact']['contact'] for the name in the view, etc.

薄荷梦 2024-09-18 11:05:56

您可以使用

$user->getAttributes() //it returns an array of data.

希望有用

You can use

$user->getAttributes() //it returns an array of data.

Hope that's usefull

人心善变 2024-09-18 11:05:56

可以使用模型关系来解决这个问题吗?您可以定义从用户模型到城市模型的关系(例如将其命名为relation_to_city),然后您可以在控制器中分配用户模型

$this->render('view', 'user'=>$user);

并访问城市(从视图)

$user->relation_to_city

It is possible to solve this using model relations? You can define a relation from the User model to the City model (e.g. naming it relation_to_city), then you can just assign the user model in the controller

$this->render('view', 'user'=>$user);

and access the city (from the view)

$user->relation_to_city
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文