CRUD NO MAGENTO 2 A PARTIR DO ZEND2
Revisão de 18h34min de 21 de fevereiro de 2018 por Sergio Mendonça (discussão | contribs)
1) Buscando a tabela no banco
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('clientela');
onde clientela é o nome da tabela
// INSERT DATA $name = 'Mukesh Chapagain'; $age = 99; $email = 'mukesh@example.com'; $sql = "INSERT INTO " . $tableName . " (id, name, age, email) VALUES (, '$name', $age, '$email')"; $connection->query($sql);
$sql = "INSERT INTO " . $tableName . " (id, name, age, email) VALUES (, 'Hello World', 88, 'hello@example.com')"; $connection->query($sql);
// SELECT DATA $sql = "SELECT * FROM " . $tableName; $result = $connection->fetchAll($sql);
echo '
'; print_r($result); echo '
';
// UPDATE DATA $id = 1; // table row id to update $sql = "UPDATE " . $tableName . " SET name = 'Your Name', email = 'your-email@example.com' WHERE id = " . $id; $connection->query($sql);
// DELETE DATA $id = 1; // table row id to delete $sql = "DELETE FROM " . $tableName . " WHERE id = " . $id; $connection->query($sql);