Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The property 'options' is always an array in Model Relations. #13989

Merged
merged 2 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- `Phalcon\Mvc\View\Simple::render()` `params` property is now always an array.
- `Phalcon\Mvc\Model\CriteriaInterface::limit()` now takes `offset` as an integer. [#13977](https://github.com/phalcon/cphalcon/pull/13977)
- Changed `Phalcon\Mvc\Model\Manager::getModelSource()` to use `setModelSource()` internally instead of setting the source manually [#13987](https://github.com/phalcon/cphalcon/pull/13987)
- The property `options` is always an array in `Phalcon\Mvc\Model\Relation`. [#13989](https://github.com/phalcon/cphalcon/pull/13989)

## Fixed
- Fixed `Mvc\Collection::isInitialized()` now works as intended. [#13931](https://github.com/phalcon/cphalcon/pull/13931)
Expand Down
51 changes: 27 additions & 24 deletions phalcon/Mvc/Model/Relation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ class Relation implements RelationInterface
/**
* Phalcon\Mvc\Model\Relation constructor
*
* @param int type
* @param string|array fields
* @param string|array referencedFields
* @param array options
*/
public function __construct(int type, string! referencedModel, var fields, var referencedFields, var options = null) -> void
public function __construct(int type, string! referencedModel, var fields, var referencedFields, array options = []) -> void
{
let this->type = type,
this->referencedModel = referencedModel,
Expand All @@ -72,14 +70,15 @@ class Relation implements RelationInterface
public function getForeignKey()
{
var options, foreignKey;

let options = this->options;
if typeof options == "array" {
if fetch foreignKey, options["foreignKey"] {
if foreignKey {
return foreignKey;
}

if fetch foreignKey, options["foreignKey"] {
if foreignKey {
return foreignKey;
}
}

return false;
}

Expand Down Expand Up @@ -118,18 +117,18 @@ class Relation implements RelationInterface
public function getOption(string! name)
{
var option;
if fetch option, this->options[name] {
return option;

if !fetch option, this->options[name] {
return null;
}
return null;

return option;
}

/**
* Returns the options
*
* @return string|array
*/
public function getOptions()
public function getOptions() -> array
{
return this->options;
}
Expand All @@ -142,14 +141,15 @@ class Relation implements RelationInterface
public function getParams()
{
var options, params;

let options = this->options;
if typeof options == "array" {
if fetch params, options["params"] {
if params {
return params;
}

if fetch params, options["params"] {
if params {
return params;
}
}

return false;
}

Expand Down Expand Up @@ -199,7 +199,9 @@ class Relation implements RelationInterface
public function isThrough() -> bool
{
var type;

let type = this->type;

return type == self::HAS_ONE_THROUGH || type == self::HAS_MANY_THROUGH;
}

Expand All @@ -209,13 +211,14 @@ class Relation implements RelationInterface
public function isReusable() -> bool
{
var options, reusable;

let options = this->options;
if typeof options == "array" {
if fetch reusable, options["reusable"] {
return reusable;
}

if !fetch reusable, options["reusable"] {
return false;
}
return false;

return reusable;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions phalcon/Mvc/Model/RelationInterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ interface RelationInterface

/**
* Returns the options
*
* @return string|array
*/
public function getOptions();
public function getOptions() -> array;

/**
* Returns parameters that must be always used when the related records are obtained
Expand Down
61 changes: 56 additions & 5 deletions tests/integration/Mvc/Model/Relation/GetFieldsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetFieldsCest
Expand All @@ -24,12 +25,62 @@ class GetFieldsCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
* @author Sid Roberts <sid@sidroberts.co.uk>
* @since 2019-04-18
*/
public function mvcModelRelationGetFields(IntegrationTester $I)
public function mvcModelRelationGetFieldsString(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getFields()');
$I->skipTest('Need implementation');
$I->wantToTest('Mvc\Model\Relation - getFields() - string');

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
[
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
]
);

$I->assertEquals(
'id',
$relation->getFields()
);
}

/**
* Tests Phalcon\Mvc\Model\Relation :: getFields()
*
* @param IntegrationTester $I
*
* @author Sid Roberts <sid@sidroberts.co.uk>
* @since 2019-04-18
*/
public function mvcModelRelationGetFieldsArray(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getFields() - array');

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
[
'type',
'name',
],
'robots_id',
[
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
]
);

$I->assertEquals(
[
'type',
'name',
],
$relation->getFields()
);
}
}
31 changes: 28 additions & 3 deletions tests/integration/Mvc/Model/Relation/GetOptionCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetOptionCest
Expand All @@ -24,12 +25,36 @@ class GetOptionCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
* @author Sid Roberts <sid@sidroberts.co.uk>
* @since 2019-04-18
*/
public function mvcModelRelationGetOption(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getOption()');
$I->skipTest('Need implementation');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);



$I->assertEquals(
$options['reusable'],
$relation->getOption('reusable')
);

$I->assertEquals(
$options['alias'],
$relation->getOption('alias')
);
}
}
24 changes: 21 additions & 3 deletions tests/integration/Mvc/Model/Relation/GetOptionsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetOptionsCest
Expand All @@ -24,12 +25,29 @@ class GetOptionsCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
* @author Sid Roberts <sid@sidroberts.co.uk>
* @since 2019-04-18
*/
public function mvcModelRelationGetOptions(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getOptions()');
$I->skipTest('Need implementation');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);

$I->assertEquals(
$options,
$relation->getOptions()
);
}
}
60 changes: 57 additions & 3 deletions tests/integration/Mvc/Model/Relation/GetParamsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetParamsCest
Expand All @@ -24,12 +25,65 @@ class GetParamsCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
* @author Sid Roberts <sid@sidroberts.co.uk>
* @since 2019-04-18
*/
public function mvcModelRelationGetParams(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getParams()');
$I->skipTest('Need implementation');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
'params' => [
'conditions' => 'robotTypeId = :type:',
'bind' => [
'type' => 4,
],
],
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);

$I->assertEquals(
$options['params'],
$relation->getParams()
);
}

/**
* Tests Phalcon\Mvc\Model\Relation :: getParams() when none are set
*
* @param IntegrationTester $I
*
* @author Sid Roberts <sid@sidroberts.co.uk>
* @since 2019-04-18
*/
public function mvcModelRelationGetParamsWhenNoneSet(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getParams() when none are set');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);

$I->assertFalse(
$relation->getParams()
);
}
}
Loading