Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to fix this storing related records

i have to table in database i tried everything research in storing related records but i can't find right solution 2 tables the

Students Model

<?php

class Students extends \Phalcon\Mvc\Model {

/**
 *
 * @var integer
 */
public $id;

/**
 *
 * @var string
 */
public $full_name;

/**
 *
 * @var string
 */
public $course;

/**
 *
 * @var string
 */
public $year;


public function initialize(){


   $this->hasMany("id","Personal","students_id",[
        'alias'=>'Personal'
    ]);
}


}

Personal Model

<?php

use Phalcon\Mvc\Model\Validator\Email as Email; use Phalcon\Db\RawValue;

/** * Personal were information of the student were stored * Validation of the creation and modified */

class Personal extends \Phalcon\Mvc\Model {

/**
 *
 * @var integer
 */
public $id;

/**
 *
 * @var string
 */
public $nick_name;

/**
 *
 * @var string
 */
public $birth_date;

/**
 *
 * @var string
 */
public $birth_place;

/**
 *
 * @var integer
 */
public $age;

/**
 *
 * @var string
 */
public $gender;

/**
 *
 * @var string
 */
public $present_address;

/**
 *
 * @var integer
 */
public $telno_a1;

/**
 *
 * @var string
 */
public $provincial_address;

/**
 *
 * @var integer
 */
public $telno_a2;

/**
 *
 * @var string
 */
public $mobile_number;

/**
 *
 * @var string
 */
public $email;

/**
 *
 * @var string
 */
public $nationality;

/**
 *
 * @var string
 */
public $religion;

/**
 *
 * @var integer
 */
public $height;

/**
 *
 * @var integer
 */
public $weight;

/**
 *
 * @var string
 */
public $created_at;

/**
 *
 * @var string
 */
public $modified_at;

/**
 *
 * @var integer
 */
public $students_id;



/**
 *
 * @var integer
 */
public $photos_id;




/**
 * Validations and business logic
 */
public function validation()
{

    $this->validate(
        new Email(
            array(
                'field'    => 'email',
                'required' => true,
            )
        )
    );
    if ($this->validationHasFailed() == true) {
        return false;
    }
}



/**
 * validation on created_at and modified_at
*/
public function initialize(){

   $this->belongsTo("students_id","Students","id",[
        'alias'=>'Students'

    ]);

}

public function beforeCreate()
{
    $this->created_at = new RawValue('now()');
}

public function beforeUpdate(){
    $this->modified_at = new RawValue('now()');
}

}

in TestController

<?php

class TestController extends \Phalcon\Mvc\Controller {

public function indexAction()
{

 $students = new Students();

 $students->full_name ='name';

 $students1= new Students();
 $students1->full_name = 'heljhum';

 $students->save();
 $students1->save();

 $personal = new Personal();
   $personal->students_id = array($students,$students1);
 $personal->nick_name = 'kuro arashi';
 $personal->gender = 'male';

    if(!$students->save()){
        $this->flash->error('not added');
    }
    else{
        $this->flash->success('added');
        //var_dump($personal);
    }


 }
public function welcomeAction(){
    echo $this->persistent->name;
}

}

thanks for the help :)



125
Accepted
answer
edited Feb '15

var_dump($students->getMessages());

Should give you some hints as to why it's not saving.

when i try to fix this i got this error

Catchable fatal error: Object of class Students could not be converted to string in C:\xampp\htdocs\guidance\app\controllers\TestController.php on line 27

Post the content of TestController?

thanks i fix this :)

its assigning a object when the data is a integer i fix the issue when i change the code in

$personal = new Personal(); $personal->students_id = $students->id;