Don't you speak portuguese? Translate this site with Google Translator

Pensamento do Dia

Todas as flores do futuro estão nas sementes de hoje. (Provérbio Chinês)

Acesso a Bancos de Dados

Acesso a Bancos de Dados no CakePHP 3

use Cake\Datasource\ConnectionManager;

Conexão
$connection = ConnectionManager::get('default');

Select
$results = $connection->execute('SELECT * FROM articles')->fetchAll('assoc');

ou
$results = $connection->execute('SELECT * FROM articles WHERE id = :id', ['id' => 1])->fetchAll('assoc');


Insert
use Cake\Datasource\ConnectionManager;

$connection = ConnectionManager::get('default');
$connection->insert('articles', [
'title' => 'A New Article',
'created' => new DateTime('now')
], ['created' => 'datetime']);

Update
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$connection->update('articles', ['title' => 'New title'], ['id' => 10]);

Delete
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$connection->delete('articles', ['id' => 10]);


$stmt = $conn->query('UPDATE posts SET published = 1 WHERE id = 2');

The query() method does not allow for additional parameters. If you need additional parameters you should use the execute() method, which allows for placeholders to be used:

$stmt = $conn->execute(
'UPDATE posts SET published = ? WHERE id = ?',
[1, 2]
);

$stmt = $conn->execute(
'UPDATE posts SET published_date = ? WHERE id = ?',
[new DateTime('now'), 2],
['date', 'integer']
);

$stmt->execute();

Ler um registro
$row = $stmt->fetch('assoc');

Ler todos os registros
$rows = $stmt->fetchAll('assoc');

Ler registros interagindo
foreach ($rows as $row) {
// Do work
}

$rowCount = count($stmt);
$rowCount = $stmt->rowCount();

Insert
$query = $articles->query();
$query->insert(['title', 'body'])
->values([
'title' => 'First post',
'body' => 'Some body text'
])
->execute();

$select = $articles->find()
->select(['title', 'body', 'published'])
->where(['id' => 3]);

$query = $articles->query()
->insert(['title', 'body', 'published'])
->values($select)
->execute()

Update
$query = $articles->query();
$query->update()
->set(['published' => true])
->where(['id' => $id])
->execute();

Delete
$query = $articles->query();
$query->delete()
->where(['id' => $id])
->execute();

Outras
$matchingComment = $articles->association('Comments')->find()
->select(['article_id'])
->distinct()
->where(['comment LIKE' => '%CakePHP%']);

$query = $articles->find()
->where(['id' => $matchingComment]);

Comments fornecido por CComment

Novo Testamento

Mas, como é santo aquele que vos chamou, sede vós também santos em toda a vossa maneira de viver;
(1Pe, 1:15)

Rotas no Mapa do Google

© 2015 Ribamar FS. All Rights Reserved. Designed By JoomShaper