for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the Tinyissue package.
*
* (c) Mohamed Alsharaf <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tinyissue\Model\Traits;
/**
* CountAttributeTrait is trait class for adding method to return count attribute.
* @author Mohamed Alsharaf <[email protected]>
* @property static $this
trait CountAttributeTrait
{
* Returns the aggregate value of a field.
* @param string $field
* @return int
protected function getCountAttribute($field)
// if relation is not loaded already, let's do it first
if (!array_key_exists($field, $this->relations)) {
relations
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
$this->load($field);
}
$related = $this->getRelation($field);
// then return the count directly
return ($related) ? (int) $related->aggregate : 0;
* Eager load relations on the model.
* @param array|string $relations
* @return $this
abstract public function load($relations);
* Get a specified relationship.
* @param string $relation
* @return mixed
abstract public function getRelation($relation);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: