Hello, I'm trying to get the advantages of the relationships in Phalcon models. I have two tables which is post and like.
each post has many likes while one like belongs to one post.
here is my models.
Like.php
<?php
namespace FC\Models;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Mvc\Model;
class Like extends Model
{
public $id;
public $postID;
public $userID;
public function initialize()
{
$this->belongsTo("post_id", "post", "id");
}
}
Post.php
<?php
namespace FC\Models;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Mvc\Model;
class Post extends Model
{
public $id;
public $content;
public $created_at;
public $likes;
public $userID;
public function initialize()
{
$this->hasMany("id", "Like", "post_id");
}
}
UserController.php
<?php
namespace FC\Controllers;
use Phalcon\Mvc\Controller,
FC\Controllers\ErrorController as Error,
FC\Models\User,
FC\Models\Post,
FC\Models\User_follow,
FC\Models\Like,
FC\Models\User_info;
class UserController extends ControllerBase
{
public function usernameAction($username)
{
$user_posts = Post::findFirst();
foreach($user_posts->likes as $like)
{
var_dump($like);
}
}
}
It shows me this error: Warning: Invalid argument supplied for foreach().
Please correct me :) thanks.