Skip to content

Commit

Permalink
Use EntityTrait->has() where possible.
Browse files Browse the repository at this point in the history
The new has() signature supports higher level code in Table operations
and lets use not use array_filter() in various different ways.
  • Loading branch information
markstory committed Aug 15, 2014
1 parent 3f0bd20 commit 22c374c
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/ORM/Table.php
Expand Up @@ -1146,12 +1146,11 @@ public function save(EntityInterface $entity, $options = []) {
*/
protected function _processSave($entity, $options) {
$primaryColumns = (array)$this->primaryKey();
$primary = $entity->extract($primaryColumns);

if ($primaryColumns && $entity->isNew() && array_filter($primary, 'strlen') === $primary) {
if ($primaryColumns && $entity->isNew() && $entity->has($primaryColumns)) {
$alias = $this->alias();
$conditions = [];
foreach ($primary as $k => $v) {
foreach ($entity->extract($primaryColumns) as $k => $v) {
$conditions["$alias.$k"] = $v;
}
$entity->isNew(!$this->exists($conditions));
Expand Down Expand Up @@ -1311,15 +1310,15 @@ protected function _newId($primary) {
* @throws \InvalidArgumentException When primary key data is missing.
*/
protected function _update($entity, $data) {
$primaryKey = $entity->extract((array)$this->primaryKey());
$data = array_diff_key($data, $primaryKey);
$primaryColumns = (array)$this->primaryKey();
$primaryKey = $entity->extract($primaryColumns);

$data = array_diff_key($data, $primaryKey);
if (empty($data)) {
return $entity;
}

$filtered = array_filter($primaryKey, 'strlen');
if (count($filtered) < count($primaryKey)) {
if (!$entity->has($primaryColumns)) {
$message = 'All primary key value(s) are needed for updating';
throw new \InvalidArgumentException($message);
}
Expand Down Expand Up @@ -1401,17 +1400,16 @@ protected function _processDelete($entity, $options) {
if ($entity->isNew()) {
return false;
}
$primaryKey = (array)$this->primaryKey();
$conditions = (array)$entity->extract($primaryKey);

if (!array_filter($conditions, 'strlen')) {
$msg = 'Deleting requires a primary key value';
$primaryKey = (array)$this->primaryKey();
if (!$entity->has($primaryKey)) {
$msg = 'Deleting requires all primary key values.';
throw new \InvalidArgumentException($msg);
}

$this->_associations->cascadeDelete($entity, $options->getArrayCopy());

$query = $this->query();
$conditions = (array)$entity->extract($primaryKey);
$statement = $query->delete()
->where($conditions)
->execute();
Expand Down

0 comments on commit 22c374c

Please sign in to comment.