We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

access to related models

Hi! I have encountered a problem while working with the related models. There are 2 tables in the database.

News (id, date, active)

Newstranslations (id, news_id, lang_id, title, preview_content, full_content)

And there are 2 models for them.

class News extends Model {
   public $id;
   public $date;
   public $active;

   public function initialize(){
           $this->hasMany("id", "Modules\Modules\Models\Newstranslations", "news_id", array(
           'alias' => 'Newstranslations',
           'foreignKey' => array(
               'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
           )));
   }
 }

class Newstranslations extends Model {
    public $id;
    public $news_id;
    public $lang_id;
    public $title;
    public $preview_content;
    public $full_content;

    public function initialize(){
        $this->belongsTo('news_id', 'Modules\Modules\Models\News', 'id', array(
            'alias' => 'News'));
    }
 }

Each table contains 1 entry. I want to get acess to the entries of the Newstranslations table from the News table, but I have encountered some errors. I use the following documentation. In the first case everything works fine.

    $test_news = News::findFirst(17);
    foreach ($test_news->Newstranslations as $news_translations) {
        $news_translations;
    }
    $this->view->setVar("test", $news_translations);*/
    In my View: {{ test.title }}

But it doesn't work when I don't use the foreach-cycle.

    $test_news = News::findFirst(17);
    $test_news->Newstranslations;
    $this->view->setVar("test", $test_news);
    In my View: {{ test.title }} 

    $test_news = News::findFirst(17);
    $test_news->getNewstranslations(array('lang_id= :id:', 'bind'=> array('id'=> 2)));
    $this->view->setVar("test", $test_news);
    In my View: {{ test.title }} 

These examples throw the following exception:

And this example:

    $test_news = News::findFirst(17);
    $test_news->getNewstranslations(array('lang_id= :id:', 'bind'=> array('id'=> 2)))->title;
    $this->view->setVar("test", $test_news);
    In my View:  {{ test }}

Why don`t the examples from the documentation work? And how can I get acess to the entries of the Newstranslations table without using the foreach-cyycle? Could you please help me?

I tried to repeat the example from the documentation:

DB:

CREATE TABLE IF NOT EXISTS `robots` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(70) NOT NULL,
  `type` varchar(32) NOT NULL,
  `year` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `robots` (`id`, `name`, `type`, `year`) VALUES
(1, 'Robot1', 'test', 2014);

CREATE TABLE IF NOT EXISTS `parts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(70) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `parts` (`id`, `name`) VALUES
(1, 'head');

CREATE TABLE IF NOT EXISTS `robots_parts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `robots_id` int(10) NOT NULL,
  `parts_id` int(10) NOT NULL,
  `created_at` date NOT NULL,
  PRIMARY KEY (`id`),
  KEY `robots_id` (`robots_id`),
  KEY `parts_id` (`parts_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `robots_parts` (`id`, `robots_id`, `parts_id`, `created_at`) VALUES
(1, 1, 1, '2014-08-15');

Models:

class Robots extends Model {

    public $id;

    public $name;

    public function initialize()
    {
        $this->hasMany("id", "Modules\Modules\Models\RobotsParts", "robots_id", array( "alias" => "RobotsParts"));
    }

    public static function find($parameters = array()){
        return parent::find($parameters);
    }

    public static function findFirst($parameters = array()){
        return parent::findFirst($parameters);
    }

    public function columnMap(){
        return array(
            'id' => 'id',
            'name' => 'name',
            'type' => 'type',
            'year' => 'year'
        );
    }
}

class Parts extends Model {

    public $id;

    public $name;

    public function initialize()
    {
        $this->hasMany("id", "Modules\Modules\Models\RobotsParts", "parts_id", array( "alias" => "RobotsParts"));
    }

    public static function find($parameters = array()){
        return parent::find($parameters);
    }

    public static function findFirst($parameters = array()){
        return parent::findFirst($parameters);
    }

    public function columnMap(){
        return array(
            'id' => 'id',
            'name' => 'name',
        );
    }
}

class RobotsParts extends Model {

    public $id;

    public $robots_id;

    public $parts_id;

    public function initialize()
    {
        $this->belongsTo("robots_id", "Modules\Modules\Models\Robots", "id", array( "alias" => "Robots"));
        $this->belongsTo("parts_id", "Modules\Modules\Models\Parts", "id", array( "alias" => "Parts"));
    }

    public static function find($parameters = array()){
        return parent::find($parameters);
    }

    public static function findFirst($parameters = array()){
        return parent::findFirst($parameters);
    }

    public function columnMap(){
        return array(
            'id' => 'id',
            'robots_id' => 'robots_id',
            'parts_id' => 'parts_id',
            'created_at' => 'created_at'
        );
    }
}

Controller:

This code works perfect:

    $robot = Robots::findFirst();
    foreach ($robot->robotsParts as $robotPart) {
        echo $robotPart->parts->name, "\n";
    }

And this:

    $robot = Robots::findFirst();
    echo $robotsParts = $robot->robotsParts->parts->name;

Or this:

    $robot = Robots::findFirst();
    $robotsParts = $robot->getRobotsParts("created_at = '2014-08-15'");
    echo $robotsParts->name;

Throws the following exception:

Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$name in S:\Server\www\game-serials\apps\modules\backend\controllers\DebugController.php on line 23

What is wrong?



98.9k
Accepted
answer
edited Aug '14

$robotsParts is a resultset, which means it returns 0 or more records, so you have to do:

$robot = Robots::findFirst();
$robotsParts = $robot->getRobotsParts("created_at = '2014-08-15'");
foreach ($robotsParts as $robotPart) {
    echo $robotPart->name;
}

Thank you. Understood, now everything is clear.