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?