Cake\ORM\Table::get PHP Method

get() public method

### Usage Get an article and some relationships: $article = $articles->get(1, ['contain' => ['Users', 'Comments']]);
public get ( $primaryKey, $options = [] )
    public function get($primaryKey, $options = [])
    {
        $key = (array) $this->primaryKey();
        $alias = $this->alias();
        foreach ($key as $index => $keyname) {
            $key[$index] = $alias . '.' . $keyname;
        }
        $primaryKey = (array) $primaryKey;
        if (count($key) !== count($primaryKey)) {
            $primaryKey = $primaryKey ?: [null];
            $primaryKey = array_map(function ($key) {
                return var_export($key, true);
            }, $primaryKey);
            throw new InvalidPrimaryKeyException(sprintf('Record not found in table "%s" with primary key [%s]', $this->table(), implode($primaryKey, ', ')));
        }
        $conditions = array_combine($key, $primaryKey);
        $cacheConfig = isset($options['cache']) ? $options['cache'] : false;
        $cacheKey = isset($options['key']) ? $options['key'] : false;
        $finder = isset($options['finder']) ? $options['finder'] : 'all';
        unset($options['key'], $options['cache'], $options['finder']);
        $query = $this->find($finder, $options)->where($conditions);
        if ($cacheConfig) {
            if (!$cacheKey) {
                $cacheKey = sprintf("get:%s.%s%s", $this->connection()->configName(), $this->table(), json_encode($primaryKey));
            }
            $query->cache($cacheKey, $cacheConfig);
        }
        return $query->firstOrFail();
    }

Usage Example

コード例 #1
1
 /**
  * testSendNewPasswordEmail
  *
  * @return void
  */
 public function testSendNewPasswordEmail()
 {
     $user = $this->Users->get(1);
     $this->Mailer->expects($this->once())->method('to')->with('*****@*****.**')->will($this->returnSelf());
     $this->Mailer->expects($this->once())->method('subject')->with('Your new password')->will($this->returnSelf());
     $this->Mailer->expects($this->once())->method('template')->with('Burzum/UserTools.Users/new_password')->will($this->returnSelf());
     $this->Mailer->expects($this->once())->method('set')->with('user', $user)->will($this->returnSelf());
     $this->Mailer->sendNewPasswordEmail($user);
 }
All Usage Examples Of Cake\ORM\Table::get