Tutorial: Populate 2 Model Information Using 1 Controller And 1 View In CRUD

Recently i have been playing with Yii to get something up for someone. However, i came into a lot of problem because i am very new to Yii framework. Hence, there is a lot of reading and try and error method for me. I faced a problem where i need to populate 2 model information into 1 view. However, the CRUD that produce the codes seems to only cater for 1 to 1 sort of relationship. Furthermore, there wasn't any good information on how to tackle this problem other than the one shown on the cook book. On the cook book, it is demonstrating a 1 to 1 model relationship. In my case, it is a 1 to many relationship. What should i do?

5 thoughts on “Tutorial: Populate 2 Model Information Using 1 Controller And 1 View In CRUD

  1. I want to ask a question that what is the statement means
    $this->loadModel()
    and I also recieve and error when putting this line of code in my actionMethod()

  2. HI this is a good solution! however i tried to implement this to a form where the user onyl creates NEW entries and i am having problems with the array.

    this is $_POST returned:

    array
    'Order' =>
    array
    'firstname' => string '' (length=0)
    'lastname' => string '' (length=0)
    'email' => string '' (length=0)
    'mobilephone' => string '' (length=0)
    'more_info' => string '0' (length=1)
    'transports_id' => string '2' (length=1)
    'Visitor' =>
    array
    'firstname' =>
    array
    0 => string '' (length=0)
    'submitDatas' => string 'Create' (length=6)

    it should be opposite right? on visitor->firstname->0 it should be visitor->0->firstname?

    here is my controller:

    public function actionReg()
    {
    // Adding jQuery to the view page.
    Yii::app()->getClientScript()->registerCoreScript('jquery');

    $order=new Order;
    $visitors = array ( new Visitor );
    var_dump($_POST);
    if( isset( $_POST['submitDatas'] ) && isset( $_POST['Order'], $_POST['Visitor'] ) )
    {
    $order->attributes=$_POST['Order'];
    $order->timeregistered = date("Y-m-d H:i:s");
    $valid=$order->validate();

    $visitor = new Visitor;
    foreach ( $_POST['Visitor'] as $i => $item) {
    if ( isset( $visitors[$i] ) )
    $visitor = $visitors[$i];
    $visitor->attributes = $item;
    $valid = $valid && $visitor->validate();
    }

    if($order->save() && $valid){
    $i = 0;
    foreach($_POST['Visitor'] as $item){
    if(isset($visitors[$i]))
    $visitor = $visitors[$i];
    $visitor->save();
    $i++;
    }
    $this->redirect(array('order','id'=>$order->id));
    }

    }

    //$this->render('reg',array('order'=>$order));
    $this->render('reg', array(
    'order' => $order,
    'visitors' => $visitors,
    'visitorNumber' => isset($_POST['Visitor']) ? count($_POST['Visitor'])-1 : 0, //How many visitors the user added
    'update' => false,
    ));

    }

  3. hehehe..update is quite simple with this concept but the challenge comes when you are creating new record tho. In order for create to work, you will need to do more than this tutorial has to offer 🙂

  4. $this->loadModel() is the default controller functions when you setup a controller on Gii. it retrieves the data from the model and pass it back to the caller 🙂

Comments are closed.