[2.0.2] Models - hasManyToMany cant accept conditions. Return error
Hi there...
To explain: usually, with other model relations, i use $obj->getParts("is_active=1") to make addition filtering. When i have relation hasManyToMany this return me an error - for example:
Can't obtain model's source from models list: 'Models\Authors', when preparing: SELECT [\Models\Authors].* FROM [\Models\Authors] INNER JOIN [\Models\EventsAuthors] ON [\Models\EventsAuthors].[author_id] = [\Models\Authors].[id] WHERE [\Models\EventsAuthors].[event_id] = ?0 AND is_photograph=1
And its obvious that (in that case) is_photograph is not proper included.
So... I think that is a bug.
Thanks for great framework!
Okay :) In the model:
class Articles extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->hasMany("id", "\Models\Pictures", "parent_id", array(
'alias' => 'Pictures',
'params' => ['order' => 'position ASC']
));
$this->hasManyToMany("id", "\Models\ArticlesAuthors", "article_id", "author_id", "\Models\Authors", "id", array(
'alias' => 'Authors'
));
}
}
Now... in the controller - i can do this:
$obj = \Models\Articles::findFirst();
$pictures = $obj->getPictures("is_active=1");
...but cant do that:
$authors = $obj->getAuthors("is_photograph=1");
If need other information, i can put whole project to github. Thanks!
I'm using 2.0.3 (branch 2.0.x) and the following script to reproduce the issue and it works:
<?php
namespace Some;
use Phalcon\DI,
Phalcon\Db\Column,
Phalcon\Db\RawValue,
Phalcon\Mvc\Model,
Phalcon\Events\Manager as EventsManager,
Phalcon\Db\Adapter\Pdo\MySQL as Connection,
Phalcon\Mvc\Model\Manager as ModelsManager,
Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData;
$eventsManager = new EventsManager();
$di = new DI();
$connection = new Connection(array(
"host" => "localhost",
"username" => "root",
"password" => "hea101",
"dbname" => "phalcon_test",
));
$connection->setEventsManager($eventsManager);
$eventsManager->attach('db',
function ($event, $connection) {
switch ($event->getType()) {
case 'beforeQuery':
echo $connection->getSqlStatement(), "\n";
print_r($connection->getSqlVariables());
break;
}
}
);
$modelsManager = new ModelsManager();
$modelsManager->setDi($di);
$di['db'] = $connection;
$di['modelsManager'] = $modelsManager;
$di['modelsMetadata'] = new ModelsMetadata();
class Robots extends Model
{
public function initialize()
{
$this->hasManyToMany(
"id",
"Some\RobotsParts",
"robots_id",
"parts_id",
"Some\Parts",
"id",
[
'alias' => 'robotsParts'
]
);
}
}
class RobotsParts extends Model
{
}
class Parts extends Model
{
}
$robot = Robots::findFirst();
foreach ($robot->getRobotsParts("[Some\Parts].name <> 'Head'") as $parts) {
echo $parts->name, PHP_EOL;
}
Outputs:
SELECT IF(COUNT(*) > 0, 1, 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'robots' AND `TABLE_SCHEMA` = DATABASE()
DESCRIBE `robots`
SELECT `robots`.`id`, `robots`.`name`, `robots`.`type`, `robots`.`year`, `robots`.`datetime`, `robots`.`text` FROM `robots` LIMIT 1
SELECT IF(COUNT(*) > 0, 1, 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'parts' AND `TABLE_SCHEMA` = DATABASE()
DESCRIBE `parts`
SELECT `parts`.`id`, `parts`.`name` FROM `parts` INNER JOIN `robots_parts` ON `robots_parts`.`parts_id` = `parts`.`id` WHERE `robots_parts`.`robots_id` = :0 AND `parts`.`name` <> 'Head'
Array
(
[:0] => 1
)
Body
Arms
I will test that, but in my case i didnt put [Some\Parts].name.... I am not sure and i didnt know that is required, because in other cases- it is not needed (hasMany, hasOne). In documentation is no information about that.
So... if that is the right way to use that type of relation- please, add it to documentation.
Anyway- i think its better to unify the ways to communicate with it.
Thank u. /sorry for my English/