Práce s hromadou dat
Apicart
Databáze:
Aplikace:
/** @var \Doctrine\DBAL\Connection $connection */
$connection = $this->connection;
$products = $connection->fetchAll('SELECT * FROM products ORDER BY id ASC');
foreach ($products as $product) {
echo sprintf(
'
<product>
<id>%d</id>
<name>%s</name>
<price>%d</price>
</product>
',
$product['id'],
$product['name'],
$product['price'],
);
}
Databáze:
Aplikace:
$statement = $connection->prepare(
'SELECT * FROM products ORDER BY id ASC',
[\PDO::ATTR_CURSOR => \PDO::CURSOR_SCROLL]
);
$statement->execute();
while ($product = $statement->fetch(\PDO::FETCH_ASSOC)) {
echo sprintf(
'
<product>
<id>%d</id>
<name>%s</name>
<price>%d</price>
</product>
',
$product['id'],
$product['name'],
$product['price'],
);
}
Databáze:
Aplikace:
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->entityManager;
$query = $entityManager->createQuery(
'SELECT p FROM App\Model\Entity\Product p ORDER BY p.id'
);
$products = $query->getArrayResult();
foreach ($products as $product) {
echo sprintf(
'
<product>
<id>%d</id>
<name>%s</name>
<price>%d</price>
</product>
',
$product->getId(),
$product->getName(),
$product->getPrice(),
);
}
Databáze:
Aplikace:
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->entityManager;
$query = $entityManager->createQuery(
'SELECT p from App\Model\Entity\Product p ORDER BY p.id'
);
$iterableResult = $query->iterate();
foreach ($iterableResult as $product) {
echo sprintf(
'
<product>
<id>%d</id>
<name>%s</name>
<price>%d</price>
</product>
',
$product[0]->getId(),
$product[0]->getName(),
$product[0]->getPrice(),
);
$entityManager->detach($product[0]);
}
Aplikace:
$xml = [ // produkty načtené z našeho XML
[
'id' => 1,
'name' => 'NATURVITA Biosil Plus 60 tablet',
'price' => 58
],
[
//...
]
];
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->entityManager;
foreach ($xml as $data) {
$product = new \App\Model\Entity\Product;
$product->setId($data['id']);
$product->setName($data['name']);
$product->setPrice($data['price']);
$entityManager->persist($product);
$entityManager->flush();
}
Aplikace:
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->entityManager;
$xml = [ // produkty načtené z našeho XML
[
'id' => 1,
'name' => 'NATURVITA Biosil Plus 60 tablet',
'price' => 58
],
[
//...
]
];
$batchSize = 100;
foreach ($xml as $index => $data) {
$product = new \App\Model\Entity\Product;
$product->setId($data['id']);
$product->setName($data['name']);
$product->setPrice($data['price']);
$entityManager->persist($product);
if (($index % $batchSize) === 0) {
$entityManager->flush();
}
$entityManager->flush();
}
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->entityManager;
$xml = [ // produkty načtené z našeho XML
[
'id' => 1,
'name' => 'NATURVITA Biosil Plus 60 tablet',
'price' => 58
],
[
//...
]
];
$batchSize = 100;
foreach ($xml as $index => $data) {
$product = new \App\Model\Entity\Product;
$product->setId($data['id']);
$product->setName($data['name']);
$product->setPrice($data['price']);
$entityManager->persist($product);
if (($index % $batchSize) === 0) {
$entityManager->flush();
$entityManager->clear();
}
$entityManager->flush();
$entityManager->clear();
}
Aplikace:
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->entityManager;
$query = $entityManager->createQuery(
'SELECT p from App\Model\Entity\Product p ORDER BY p.id'
);
$iterableResult = $query->iterate();
$batchSize = 100;
$i = 0;
foreach ($iterableResult as $row) {
$product = $row[0];
$product->increasePrice(10);
if (($i % $batchSize) === 0) {
$entityManager->flush();
$entityManager->clear();
}
++$i;
}
$entityManager->flush();
tomaspilar.cz | @tomaspilaru