Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Commit

Permalink
patch alg
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Apr 19, 2017
1 parent 77b9b83 commit b518671
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": "^7.0",
"doctrine/inflector": "~1.0",
"deimos/database": "~2.0"
"deimos/database": "~2.0",
"phpixie/orm": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
Expand Down
2 changes: 1 addition & 1 deletion demo/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Asd extends \Deimos\ORM\Entity

$user = $orm->repository('user')
// ->orderBy('id', 'DESC')
->findOne();
->findOne(['images', 'asd', 'roles']);

if (!$user)
{
Expand Down
20 changes: 15 additions & 5 deletions src/ORM/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,21 @@ protected function manyToMany(array &$config, array &$arguments)
$leftRightModel = $config['modelId'] ?: $model . ucfirst($pkModel);
$leftRightFrom = $config['itemId'] ?: $from . ucfirst($pkFrom);

return $this->orm->repository(['right' => $model])
->select(['right.*'])
->join(['leftRight' => $table])->inner()
->on('right.' . $pkModel, 'leftRight.' . $leftRightModel)
->where('leftRight.' . $leftRightFrom, $this->id());
// SELECT `right`.`*` FROM `roles` AS `right` INNER JOIN `usersRoles` AS `leftRight` ON (`right`.`id` = `leftRight`.`roleId`) WHERE ( (`leftRight`.`userId` = '1') )

$ids = $this->orm->repository($table)
->select($leftRightModel)
->where($leftRightFrom, $this->id())
->find(false);

return $this->orm->repository($model)
->where($this->orm->mapPK($model), array_map('current', $ids));

// return $this->orm->repository(['right' => $model])
// ->select(['right.*'])
// ->join(['leftRight' => $table])->inner()
// ->on('right.' . $pkModel, 'leftRight.' . $leftRightModel)
// ->where('leftRight.' . $leftRightFrom, $this->id());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/ORM/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class ORM
*/
protected $tableMap = [];


/**
* @var string[]
*/
Expand Down Expand Up @@ -94,8 +93,8 @@ public function setConfig(array $storage)
{
foreach ($storage as $modelName => $config)
{
$class = isset($config['class']) ? $config['class'] : Entity::class;
$relations = isset($config['relations']) ? $config['relations'] : [];
$class = $config['class'] ?? Entity::class;
$relations = $config['relations'] ?? [];

$this->register($modelName, $class, $relations);
}
Expand Down
14 changes: 11 additions & 3 deletions src/ORM/Queries/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ public function find($asObject = true)
}

/**
* @param array $asObject
* @param array|boolean $preload
*
* @return array|Entity
*/
public function findOne($asObject = [])
public function findOne($preload = [])
{
if (false !== $asObject)
if (false !== $preload)
{
$self = clone $this;
$self->limit(1);
Expand All @@ -133,6 +133,14 @@ public function findOne($asObject = [])
$object();
$object->setModelName($this->modelName);

if (is_array($preload))
{
foreach ($preload as $load)
{
$object->$load;
}
}

$sth->closeCursor();

return $object;
Expand Down

0 comments on commit b518671

Please sign in to comment.