Hi,
I'm extending the Uniqueness validator for use in a model. When the validator is run for a table update, I'm getting the following error:
Phalcon\Mvc\Model\Exception: Column 'primaryKey' doesn't belong to any of the selected models (1), when preparing: SELECT COUNT(*) AS rowcount FROM [Test] WHERE mappedData = ?0 AND primaryKey <> ?1
I am using a columnMap for my model and when the query is constructed the mapped column name is used but not the mapped primary key name. This only happens when using my extension of the uniqueness validator. If I use the parent class directly there is no problem.
The validator in my sample code simply calls the parent validate method and it still fails. Am I running into some PHP inheritance issue here?
Sample code below:
<?php
error_reporting( E_ALL );
function printError( $inError )
{
echo "<pre>";
echo get_class( $inError ), ": ", $inError->getMessage(), "\n\n";
echo "Phalcon Version: ", Phalcon\Version::get(), "\n";
echo "File: ", $inError->getFile(), "\n";
echo "Line: ", $inError->getLine(), "\n\n";
echo $inError->getTraceAsString();
echo "</pre>";
}
class ExtendedUniquenessValidator extends Phalcon\Validation\Validator\Uniqueness
{
public function validate( Phalcon\Validation $inValidation, $inField )
{
return parent::validate( $inValidation, $inField );
}
}
class Test extends \Phalcon\Mvc\Model
{
public $mappedPrimaryKey;
public $mappedData;
public function columnMap()
{
// Keys are the real names in the table and
// the values their names in the application
//
return array(
"primaryKey" => "mappedPrimaryKey",
"data" => "mappedData"
);
}
public function validation()
{
$validator = new Phalcon\Validation();
//$uv = new Phalcon\Validation\Validator\Uniqueness( array("message" => "Data must be unique" )); // works
$uv = new ExtendedUniquenessValidator( array("message" => "Data must be unique" )); // doesn't work
$validator->add( "mappedData", $uv );
return $this->validate( $validator );
}
}
try
{
$di = new Phalcon\DI\FactoryDefault();
$di->setShared('db', function () {
return new \Phalcon\Db\Adapter\PDO\Sqlite(array(
'dbname' => ':memory:'
));
});
$di['db']->createTable(
'test',
null,
[
'columns' => [
new \Phalcon\Db\Column(
'primaryKey',
[
'type' => \Phalcon\Db\Column::TYPE_INTEGER,
'notNull' => true,
'autoIncrement' => true,
'primary' => true,
]
),
new \Phalcon\Db\Column(
'data',
[
'type' => \Phalcon\Db\Column::TYPE_TEXT,
'notNull' => true,
]
)
]
]
);
$testObject1 = new Test;
$testObject1->mappedData = "Object 1";
$testObject1->save();
$result = Test::findFirst();
$result->mappedData = "Object 2";
$result->save(); // Triggers exception
echo "end";
}
catch ( \Exception $e )
{
printError( $e );
}
catch ( \Error $e )
{
printError( $e );
}