我正在尝试通过以下代码获取与类别相关的视频博客,但在 var_dump 中却什么也没得到?我想获取与类别相关的视频:
$category = VideoBlogCategoryModel::findFirst(1); // This returns category successfully and there are many video blogs having this category linked
var_dump($category->getVideoBlogs());exit;
VideoBlogModel.php
public function initialize(){
// Run base initialize code
parent::initialize();
// Configure Relation with VideoBlogCategoryModel
$this->belongsTo('category_id', VideoBlogCategoryModel::class, 'id', array(
'alias' => 'videoCategory',
'foreignKey' => true
));
}
public function getVideoCategory(){
return $this->videoCategory;
}
public function setVideoCategory($videoCategory){
$this->videoCategory = $videoCategory;
}
VideoBlogCategoryModel.php
public function initialize(){
// Run base initialize code
parent::initialize();
// Configure relation with VideoBlogModel
$this->hasMany('id', VideoBlogModel::class, 'category_id', array(
'alias' => 'videoBlogs',
'foreignKey' => true,
'cxAction' => static::ACTION_CASCADE_DELETE
));
}
public function getVideoBlogs(){
return $this->videoBlogs;
}
public function setVideoBlogs($videoBlogs){
$this->videoBlogs = $videoBlogs;
}
如果需要其他条件,请与我分享。
答案 0 :(得分:1)
在VideoBlogCategoryModel.php
中进行更改
public function getVideoBlogs() {
return $this->videoBlogs;
}
到
public function getVideoBlogs() {
return $this->getRelated('videoBlogs');
}
然后尝试按以下方式访问它:
$category = VideoBlogCategoryModel::findFirst(1);
$videos = $category->getVideoBlogs();
foreach( $videos as $video ) {
// access data here
var_dump($video->anyProperty()); // e.g $video->getId()
}
答案 1 :(得分:0)
你可以尝试
$category = VideoBlogCategoryModel::findFirst(1);
$videos = $category->getVideoBlogs();
var_dump($videos->count());
var_dump($videos->toArray());
exit;
我认为将var_dump
用于Phalcon收集对象不是一个好主意,您可以将其转换为Array和Var_dump
希望它可以为您提供帮助
答案 2 :(得分:0)
或尝试:
$categories = VideoBlogCategoryModel::findById($id);