i want to create relationship between users table and product table but the result returns false this is user model :
class Users extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $password;
public $email;
public function initialize()
{
$this->setSchema("asulaiman");
$this->setSource("users");
$this->hasMany(
'id',
'Products',
'product_id'
);}}
and this is my product model :
class Products extends \Phalcon\Mvc\Model
{
public $id;
public $user_id;
public $name;
public function initialize()
{
$this->setSchema("asulaiman");
$this->setSource("products");
$this->belongsTo(
'product_id',
'Users',
'id'
);}}
and this query :
public function showAction($id)
{
$get_product = Products::findFirst('id = ' . $id);
if (!empty($get_product)) {
$this->response->setJsonContent(array('data' => $get_product->users));
return $this->response;
}}
Any help please
This is what happens when no relationship is found.
I'm going to assume your users have many products and those products belong to the users. What your code on the Products model is saying is the products_id
field on the Products
model relates to the id
field on the User
model.
The problem is that there is no product_id
field on the Products
model.
$this->belongsTo(
'user_id' // "id" field on the Products model
'Users', // The model I want
'id'// The field I want on that model (Users.id)
);
What you're doing doesn't really make a lot of sense. Besides explaining how the relationships work, I can't really help you any further without seeing the schema.