hi, i´m new to Phalcon and i´m not sure how to get data from a manytomany relationship. I have three tables: Clients, Tags and ClientsTags. mysql tables:
DROP TABLE IF EXISTS `clients`;
CREATE TABLE IF NOT EXISTS `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50),
`preview_pic` VARCHAR(200),
`short_description` VARCHAR(250),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tags`;
CREATE TABLE IF NOT EXISTS `tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) UNIQUE ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE If EXISTS `clients_tags`;
CREATE TABLE IF NOT EXISTS `clients_tags` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`client_id` INT(11) NOT NULL,
`tag_id` INT(11) NOT NULL,
PRIMARY KEY (`id`) ,
FOREIGN KEY (`client_id`) REFERENCES clients (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (`tag_id`) REFERENCES tags (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
The Models are:
Clients:
public function initialize()
{
$this->hasManytoMany(
'id',
'ClientsTags',
'client_id', 'tag_id',
'Tags',
'id',
array(
'alias'=>'tags',
'foreignKey' => array(
'action' => Relation::ACTION_CASCADE
)
)
);
}
Tags:
public function initialize()
{
$this->hasManytoMany(
'id',
'ClientsTags',
'tag_id', 'client_id',
'Clients',
'id',
array(
'alias'=>'clients',
'foreignKey' => array(
'action' => Relation::ACTION_CASCADE
)
)
);
}
ClientsTags:
public function initialize()
{
$this->belongsTo(
'client_id',
'App\Models\Clients',
'id',
array(
'alias'=>'client',
'foreignKey' => [
'message' => 'client id does not exist or is currenty invalid'
]
)
);
$this->belongsTo(
'tag_id',
'App\Models\Tags',
'id',
array(
'alias'=>'tag',
'foreignKey' => [
'message' => 'tag id does not exist or is currenty invalid'
]
)
);
}
Now i want all clients with a specifig tag. I´m not sure how to do. I would do something like that:
public function getClientsWithTag($tag)
{
try{
$tags = Tags::find(
[
'name = :tag:',
'bind' => ['tag' => $tag]
]);
// what have i to do next??
} catch(\PDOException $e) {
}
}
Thanks for your help