A)'exclude' => "mode != 'employer'"
$id = new Zend_Form_Element_Select("id");
$id->setRegisterInArrayValidator(false);
$id->addValidator(new Zend_Validate_Db_RecordExists(array(
'table' => 'villains',
'field' => 'id',
'exclude' => "mode != 'employer'"
)));
次のクエリを生成します。
SELECT `villains`.`id`
FROM `villains`
WHERE (`id` = :value) AND (mode != 'employer')
LIMIT 1
B)'exclude' => "mode = 'employer'"
$id = new Zend_Form_Element_Select("id");
$id->setRegisterInArrayValidator(false);
$id->addValidator(new Zend_Validate_Db_RecordExists(array(
'table' => 'villains',
'field' => 'id',
'exclude' => "mode = 'employer'"
)));
次のクエリを生成します。
SELECT `villains`.`id`
FROM `villains`
WHERE (`id` = :value) AND (mode = 'employer')
LIMIT 1
ハ)'exclude' => array("field" => "mode", "value" => "employer")
$id = new Zend_Form_Element_Select("id");
$id->setRegisterInArrayValidator(false);
$id->addValidator(new Zend_Validate_Db_RecordExists(array(
'table' => 'villains',
'field' => 'id',
'exclude' => array(
"field" => "mode",
"value" => "employer"
)
)));
次のクエリを生成します。
SELECT `villains`.`id`
FROM `villains`
WHERE (`id` = :value) AND (`mode` != 'employer')
LIMIT 1
結果
Bが必要です。これは紛らわしく、間違いなくコンポーネントのロジックと動作は逆です。それにもかかわらず、必要な動作は例 B のものです。
付録
上記が期待どおりに機能することを確認するために、テストをハックすることができます (つまり、一緒にハックすることを意味します)。
test1 と test2 の両方がパスしますが、プロバイダーからわかるように、どちらも異なる結果を生成します。
class SO14706653Test extends PHPUnit_Framework_TestCase
{
/**
* @var Zend_Test_PHPUnit_Db_Connection
*/
public $dbConnection;
public function getRowCount($tableName) {
$query = "SELECT COUNT(*) FROM ".$this->dbConnection->quoteSchemaObject($tableName);
return (int) $this->dbConnection->getConnection()->query($query)->fetchColumn();
}
// hack a very quick setup for tests
public function setup() {
$app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$app->bootstrap();
$dbAdapter = $app->getBootstrap()->getResource('db'); /* @var $db Zend_Db_Adapter_Pdo_Mysql */
$this->dbConnection = new Zend_Test_PHPUnit_Db_Connection($dbAdapter, 'unittests');
$dbAdapter->exec("CREATE TABLE IF NOT EXISTS `villains` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`mode` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1");
$dbAdapter->exec('DELETE FROM villains'); // clean out db data
$dbAdapter->exec("
INSERT INTO `villains` VALUES(1, 'Joker', 'employer');
INSERT INTO `villains` VALUES(2, 'Kingpin', '');
INSERT INTO `villains` VALUES(3, 'Penguin', '');
");
}
// ensure the above setup is working as expected
public function assertPreConditions() {
$this->assertEquals(3, $this->getRowCount('villains'));
}
public function provideTest1()
{
return [
// form data is valid? isRequired?
[['id' => '1'], false, false],
[['id' => '2'], true, false],
[['id' => '3'], true, false],
[['id' => ''], true, false],
[[], true, false],
[['id' => '856'], false, false],
[['id' => '856'], false, true],
[['id' => ''], false, true],
[[], false, true],
];
}
public function provideTest2()
{
return [
// form data is valid? isRequired?
[['id' => '1'], true, false],
[['id' => '2'], false, false],
[['id' => '3'], false, false],
[['id' => ''], true, false],
[[], true, false],
[['id' => '856'], false, false],
[['id' => '856'], false, true],
[['id' => ''], false, true],
[[], false, true],
];
}
/**
* @dataProvider provideTest1
*/
public function test1(array $data, $isValid, $isRequired)
{
$form = new Zend_Form();
$id = new Zend_Form_Element_Select("id");
$id->setRegisterInArrayValidator(false);
$id->addValidator(new Zend_Validate_Db_RecordExists(array(
'table' => 'villains',
'field' => 'id',
'exclude' => "mode != 'employer'"
)));
$id->setRequired($isRequired);
$form->addElement($id);
// produces the query
// SELECT `villains`.`id`
// FROM `villains`
// WHERE (`id` = :value) AND (mode != 'employer')
// LIMIT 1
$this->assertSame($isValid, $form->isValid($data));
}
/**
* @dataProvider provideTest2
*/
public function test2(array $data, $isValid, $isRequired)
{
$form = new Zend_Form();
$id = new Zend_Form_Element_Select("id");
$id->setRegisterInArrayValidator(false);
$id->addValidator(new Zend_Validate_Db_RecordExists(array(
'table' => 'villains',
'field' => 'id',
'exclude' => "mode = 'employer'"
)));
$id->setRequired($isRequired);
$form->addElement($id);
// produces the query
// SELECT `villains`.`id`
// FROM `villains`
// WHERE (`id` = :value) AND (mode = 'employer')
// LIMIT 1
$this->assertSame($isValid, $form->isValid($data));
}
}