CakePHP3: Fetch a particular column from all rows

Say we have the following data in a table called countries:

id country capitol
1 USA Washington DC
2 Japan Tokyo
3 United Kingdom London


And you want to fetch data like this:

['Washington DC', 'Tokyo', 'London']


Then do this:

$capitols = $this->countries
->find()
->extract('capitol')
->toArray();

#cakephp3

ワオ!と言っているユーザー

CakePHP3: Get the full URL of the current page

Method 1: Use the URL helper


$url = $this->Url->build([
"controller" => "Posts",
"action" => "search",
"?" => ["foo" => "bar"]
],true);


Method 2: Use the Router


use Cake\Routing\Router;

$url = Router::url([
'controller' => 'Posts',
'action' => 'search',
'?' => ['foo' => $bar]
], true);
#cakephp3

ワオ!と言っているユーザー

CakePHP3: Check if record exists

Here is a handy way to do it:

To check by primary key:


if ($this->Users->exists($id)) {
}


To find by specifying where clause:


if ($this->Users->exists(['email' => $email])) {
}


Much more convenient than using find('count').
#cakephp3

ワオ!と言っているユーザー

Bash: working with filenames containing spaces

At times, I'd like to perform an action against all files in a directory like this:


for i in `find . -type f`
do
cp "${i}" "${i}.bak"
done


This fails when filenames contain spaces, as bash uses whitespace, tab and LF as the default delimiter. Solution? Do this:


SAVEIFS=$IFS
IFS=$(echo -en "\n\b")


#bash

ワオ!と言っているユーザー

×
  • ブログルメンバーの方は下記のページからログインをお願いいたします。
    ログイン
  • まだブログルのメンバーでない方は下記のページから登録をお願いいたします。
    新規ユーザー登録へ