Skip to content

Commit

Permalink
Merge pull request #12068 from CrNix/3.0.x-vkey-fix
Browse files Browse the repository at this point in the history
Fixed virtual foreign key check
  • Loading branch information
sergeyklay authored Aug 4, 2016
2 parents 56060d7 + c0af7cb commit 43d6121
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Fixed `Phalcon\Mvc\Model\Manager::getRelationRecords` to correct using multi relation column [#12035](https://github.com/phalcon/cphalcon/issues/12035)
- Fixed `Phalcon\Acl\Resource`. Now it implements `Phalcon\Acl\ResourceInterface` [#11959](https://github.com/phalcon/cphalcon/issues/11959)
- Fixed `save` method for all cache backends. Now it updates the `_lastKey` property correctly [#12050](https://github.com/phalcon/cphalcon/issues/12050)
- Fixed virtual foreign key check when having multiple keys [#12071](https://github.com/phalcon/cphalcon/issues/12071)

# [3.0.0](https://github.com/phalcon/cphalcon/releases/tag/v3.0.0) (2016-07-29)
- PHP 5.3 and 5.4 are now fully deprecated
Expand Down
3 changes: 2 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
position, bindParams, extraConditions, message, fields,
referencedFields, field, referencedModel, value, allowNulls;
int action, numberNull;
boolean error, validateWithNulls = false;
boolean error, validateWithNulls;

/**
* Get the models manager
Expand All @@ -1509,6 +1509,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
let error = false;
for relation in belongsTo {

let validateWithNulls = false;
let foreignKey = relation->getForeignKey();
if foreignKey !== false {

Expand Down
59 changes: 59 additions & 0 deletions tests/_data/models/BodyParts/Body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Phalcon\Test\Models\BodyParts;

use Phalcon\Mvc\Model;

/**
* \Phalcon\Test\Models\Body
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Radek Crlík <[email protected]>
* @package Phalcon\Test\Models\BodyParts
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to [email protected]
* so that we can send you a copy immediately.
*/
class Body extends Model
{

public $id;
public $head_1_id;
public $head_2_id;

public function initialize()
{
$this->setSource('issue12071_body');

$this->belongsTo(
'head_1_id',
'\Phalcon\Test\Models\BodyParts\Head',
'id',
[
'alias' => 'head1',
"foreignKey" => [
"allowNulls" => true,
"message" => "First head does not exists"
]
]
);

$this->belongsTo(
'head_2_id',
'\Phalcon\Test\Models\BodyParts\Head',
'id',
[
'alias' => 'head2',
"foreignKey" => [
"allowNulls" => true,
"message" => "Second head does not exists"
]
]
);
}
}
31 changes: 31 additions & 0 deletions tests/_data/models/BodyParts/Head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Phalcon\Test\Models\BodyParts;

use Phalcon\Mvc\Model;

/**
* \Phalcon\Test\Models\Head
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Radek Crlík <[email protected]>
* @package Phalcon\Test\Models\BodyParts
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to [email protected]
* so that we can send you a copy immediately.
*/
class Head extends Model
{

public $id;

public function initialize()
{
$this->setSource('issue12071_head');
}
}
24 changes: 24 additions & 0 deletions tests/_data/schemas/mysql/mysql.dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,30 @@ UNLOCK TABLES;

/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `issue12071_head`;
CREATE TABLE `issue12071_head` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

LOCK TABLES `issue12071_head` WRITE;
/*!40000 ALTER TABLE `issue12071_head` DISABLE KEYS */;
INSERT INTO `issue12071_head` VALUES (1);
INSERT INTO `issue12071_head` VALUES (2);
/*!40000 ALTER TABLE `issue12071_head` ENABLE KEYS */;
UNLOCK TABLES;

DROP TABLE IF EXISTS `issue12071_body`;
CREATE TABLE `issue12071_body` (
`id` INT NOT NULL AUTO_INCREMENT,
`head_1_id` INT,
`head_2_id` INT,
PRIMARY KEY (`id`),
CONSTRAINT `issue12071_body_head_1_fkey` FOREIGN KEY (`head_1_id`) REFERENCES `issue12071_head` (`id`),
CONSTRAINT `issue12071_body_head_2_fkey` FOREIGN KEY (`head_2_id`) REFERENCES `issue12071_head` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Db/Adapter/Pdo/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function () {
'artists',
'childs',
'customers',
'issue12071_body',
'issue12071_head',
'issue_11036',
'issue_1534',
'issue_2019',
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/Mvc/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Phalcon\Test\Models\PackageDetails;
use Phalcon\Mvc\Model\Resultset\Simple;
use Phalcon\Test\Models\AlbumORama\Albums;
use Phalcon\Test\Models\BodyParts\Body;

/**
* \Phalcon\Test\Unit\Mvc\Model\ManagerTest
Expand Down Expand Up @@ -157,4 +158,33 @@ function () {
}
);
}

/**
* Tests virtual foreign keys.
*
* When having multiple virtual foreign keys, check of the first one should
* affect the check of the next one.
*
* @issue 12071
* @author Radek Crlik <[email protected]>
* @since 2016-08-03
*/
public function testInvalidVirtualForeignKeys()
{
$this->specify(
'The Model::save with multiple virtual foreign keys and invalid entity',
function () {
$body = new Body();

$body->head_1_id = null;
$body->head_2_id = 999;

// PDOException should'n be thrown
expect($body->save())->equals(false);

expect($body->getMessages())->count(1);
expect($body->getMessages()[0]->getMessage())->equals('Second head does not exists');
}
);
}
}

0 comments on commit 43d6121

Please sign in to comment.