Skip to content

Instantly share code, notes, and snippets.

@GavinMcL
Created November 16, 2015 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GavinMcL/4d58e9ee2182ede3a7da to your computer and use it in GitHub Desktop.
Save GavinMcL/4d58e9ee2182ede3a7da to your computer and use it in GitHub Desktop.
tests for issue 11133, delete on simple resultset;
<?php
/**
* phalcon github issue #11133
* test-11133-novfk
*
* shows PDOException thrown on contraint violation when Models don't define
* relationships as foreign keys when calling simpleresultset->delete()
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
try {
$di = new \Phalcon\Di\FactoryDefault();
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'test',
'charset' => 'utf8'
]);
$di->set('db', $connection);
if (!$connection->tableExists('robots_11133', 'test')) {
$connection->execute('create table robots_11133 ('
. ' id int(10) not null, '
. ' name varchar(24), '
. ' type varchar(24), '
. ' PRIMARY KEY (id)) engine=InnoDB;');
}
if (!$connection->tableExists('parts_11133', 'test')) {
$connection->execute('create table parts_11133 ('
. ' id int(10) not null, '
. ' name varchar(24), '
. ' PRIMARY KEY (id)) engine=InnoDB;');
}
if (!$connection->tableExists('robots_parts_11133', 'test')) {
$connection->execute('create table robots_parts_11133 ('
. ' id int(10) not null, '
. ' robots_id int(10) not null, '
. ' parts_id int(10) not null, '
. ' PRIMARY KEY (id)) engine=InnoDB;');
$connection->execute('alter table robots_parts_11133 '
. ' add key idx_robots_11133_id (robots_id), '
. ' add key idx_parts_11133_id (parts_id);');
$connection->execute('alter table robots_parts_11133 '
. ' add constraint robots_parts_11133_ibfk_1 foreign key (robots_id) references robots_11133 (id) '
. ' on delete no action on update no action, '
. ' add constraint robots_parts_11133_ibfk_2 foreign key (parts_id) references parts_11133 (id) '
. ' on delete no action on update no action;');
}
$connection->execute(""
. " use test;"
. " set foreign_key_checks=0;"
. " truncate robots_11133; truncate parts_11133; truncate robots_parts_11133;"
. " insert into robots_11133 values "
. " (1,'Robotina','mechanical'),"
. " (2,'Astro Boy','mechanical'),"
. " (3,'Terminator','cyborg');"
. " insert into parts_11133 values "
. " (1,'head'),"
. " (2,'body'),"
. " (3,'arms'),"
. " (4,'legs'),"
. " (5,'cpu');"
. " insert into robots_parts_11133 values (1,1,1);"
. " set foreign_key_checks=1;");
class Robots extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $type;
public function initialize()
{
$this->setSource('robots_11133');
$this->hasMany('id', 'RobotsParts', 'robots_id', ['alias' => 'RobotsParts']);
}
}
class Parts extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public function initialize()
{
$this->setSource('parts_11133');
$this->hasMany('id', 'RobotsParts', 'parts_id', ['alias' => 'RobotsParts']);
}
}
class RobotsParts extends \Phalcon\Mvc\Model
{
public $id;
public $robots_id;
public $parts_id;
public function initialize()
{
$this->setSource('robots_parts_11133');
$this->belongsTo('parts_id', 'Parts', 'id', ['alias' => 'Parts']);
$this->belongsTo('robots_id', 'Robots', 'id', ['alias' => 'Robots']);
}
}
header('Content-Type: text/plain');
$robots1 = Robots::find([
'conditions' => 'type = ?1',
'bind' => [1 => 'cyborg']
]);
// try delete all cyborgs - no parts relations, so returns true
echo 'deleting cyborgs...'.PHP_EOL;
if ($robots1->delete() === true) {
echo 'deleted all cyborgs'.PHP_EOL;
} else {
echo 'failed to delete all cyborgs'.PHP_EOL;
}
if (!empty($robots1->getMessages())) {
foreach ($robots1->getMessages() as $message) {
echo " {$message}".PHP_EOL;
}
}
echo PHP_EOL;
$robots2 = Robots::find([
'conditions' => 'type = ?1',
'bind' => [1 => 'mechanical']
]);
// try deleting all mechanicals, except Robotina has a part
// relation so a PDOException will be caught at the end of the file.
echo 'deleting mechanicals...'.PHP_EOL;
if ($robots2->delete() === true) {
echo 'deleted all mechanicals'.PHP_EOL;
} else {
echo 'failed to delete all mechanicals'.PHP_EOL;
}
if (!empty($robots2->getMessages())) {
foreach ($robots2->getMessages() as $message) {
echo " {$message}".PHP_EOL;
}
}
} catch (\Exception $ex) {
echo $ex->getMessage();
die();
}
<?php
/**
* phalcon github issue #11133
* test-11133-withvfk
*
* shows when Models do define relationships as foreign keys that the call simpleresultset->delete()
* returns true (indicating success) even though the constraint violation has prevented delete, and
* has left a message in simpleresultset->getMessages()
*
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
try {
$di = new \Phalcon\Di\FactoryDefault();
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'test',
'charset' => 'utf8'
]);
$di->set('db', $connection);
if (!$connection->tableExists('robots_11133', 'test')) {
$connection->execute('create table robots_11133 ('
. ' id int(10) not null, '
. ' name varchar(24), '
. ' type varchar(24), '
. ' PRIMARY KEY (id)) engine=InnoDB;');
}
if (!$connection->tableExists('parts_11133', 'test')) {
$connection->execute('create table parts_11133 ('
. ' id int(10) not null, '
. ' name varchar(24), '
. ' PRIMARY KEY (id)) engine=InnoDB;');
}
if (!$connection->tableExists('robots_parts_11133', 'test')) {
$connection->execute('create table robots_parts_11133 ('
. ' id int(10) not null, '
. ' robots_id int(10) not null, '
. ' parts_id int(10) not null, '
. ' PRIMARY KEY (id)) engine=InnoDB;');
$connection->execute('alter table robots_parts_11133 '
. ' add key idx_robots_11133_id (robots_id), '
. ' add key idx_parts_11133_id (parts_id);');
$connection->execute('alter table robots_parts_11133 '
. ' add constraint robots_parts_11133_ibfk_1 foreign key (robots_id) references robots_11133 (id) '
. ' on delete no action on update no action, '
. ' add constraint robots_parts_11133_ibfk_2 foreign key (parts_id) references parts_11133 (id) '
. ' on delete no action on update no action;');
}
$connection->execute(""
. " use test;"
. " set foreign_key_checks=0;"
. " truncate robots_11133; truncate parts_11133; truncate robots_parts_11133;"
. " insert into robots_11133 values "
. " (1,'Robotina','mechanical'),"
. " (2,'Astro Boy','mechanical'),"
. " (3,'Terminator','cyborg');"
. " insert into parts_11133 values "
. " (1,'head'),"
. " (2,'body'),"
. " (3,'arms'),"
. " (4,'legs'),"
. " (5,'cpu');"
. " insert into robots_parts_11133 values (1,1,1);"
. " set foreign_key_checks=1;");
class Robots extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $type;
public function initialize()
{
$this->setSource('robots_11133');
$this->hasMany('id', 'RobotsParts', 'robots_id', ['alias' => 'RobotsParts', 'foreignKey' => true]);
}
}
class Parts extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public function initialize()
{
$this->setSource('parts_11133');
$this->hasMany('id', 'RobotsParts', 'parts_id', ['alias' => 'RobotsParts', 'foreignKey' => true]);
}
}
class RobotsParts extends \Phalcon\Mvc\Model
{
public $id;
public $robots_id;
public $parts_id;
public function initialize()
{
$this->setSource('robots_parts_11133');
$this->belongsTo('parts_id', 'Parts', 'id', ['alias' => 'Parts']);
$this->belongsTo('robots_id', 'Robots', 'id', ['alias' => 'Robots']);
}
}
header('Content-Type: text/plain');
$robots1 = Robots::find([
'conditions' => 'type = ?1',
'bind' => [1 => 'cyborg']
]);
// try delete all cyborgs - no parts relations, so returns true
// and no messages
echo 'deleting cyborgs...'.PHP_EOL;
if ($robots1->delete() === true) {
echo 'deleted all cyborgs'.PHP_EOL;
} else {
echo 'failed to delete all cyborgs'.PHP_EOL;
}
if (!empty($robots1->getMessages())) {
foreach ($robots1->getMessages() as $message) {
echo " {$message}".PHP_EOL;
}
}
echo PHP_EOL;
$robots2 = Robots::find([
'conditions' => 'type = ?1',
'bind' => [1 => 'mechanical']
]);
// try deleting all mechanicals, except Robotina has a head, so
// the foreign key relation should cause a return of false, except
// it returns true (but still created the correct error message)
echo 'deleting mechanicals...'.PHP_EOL;
if ($robots2->delete() === true) {
echo 'deleted all mechanicals'.PHP_EOL;
} else {
echo 'failed to delete all mechanicals'.PHP_EOL;
}
if (!empty($robots2->getMessages())) {
foreach ($robots2->getMessages() as $message) {
echo " {$message}".PHP_EOL;
}
}
} catch (\Exception $ex) {
echo $ex->getMessage();
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment