PHP: doctrine sample
debug doctrine count querys
/*
DROP TABLE IF EXISTS shop;
CREATE TABLE shop (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 4
AVG_ROW_LENGTH = 5461
CHARACTER SET cp1251
COLLATE cp1251_general_ci;
DROP TABLE IF EXISTS goods;
CREATE TABLE goods (
id INT(11) NOT NULL AUTO_INCREMENT,
id_shop INT(11) DEFAULT NULL,
name VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT FK_goods_shop_id FOREIGN KEY (id_shop)
REFERENCES shop(id) ON DELETE RESTRICT ON UPDATE RESTRICT
)
ENGINE = INNODB
AUTO_INCREMENT = 6
AVG_ROW_LENGTH = 3276
CHARACTER SET cp1251
COLLATE cp1251_general_ci;
INSERT INTO shop VALUES
(1, 'Shop A'),
(2, 'Shop B'),
(3, 'Shop C');
INSERT INTO goods VALUES
(1, 1, 'Goods 1 - A'),
(2, 1, 'Goods 2 - A'),
(3, 2, 'Goods 1 - B'),
(4, 2, 'Goods 2 - B'),
(5, 3, 'Goods 1 - C');
*/
require_once "composer/vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/");
$isDevMode = false;
// the connection configuration
$dbParams = array (
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'test',
'dbname' => 'test',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$logger = new \Doctrine\DBAL\Logging\DebugStack();
$config->setSQLLogger($logger);
$entityManager = EntityManager::create($dbParams, $config);
/**
* @Entity
* @Table(name="shop")
*/
class Shop {
/** @Id @GeneratedValue @Column(type="integer") */
public $id;
/** @Column(type="string") */
public $name;
/**
* @OneToMany(targetEntity="Goods", mappedBy="shop")
*/
public $goods;
public function __construct() {
}
}
/**
* @Entity
* @Table(name="goods")
*/
class Goods {
/** @Id @GeneratedValue @Column(type="integer") */
public $id;
/** @Column(name="id_shop")*/
public $idShop;
/**
* @ManyToOne(targetEntity="Shop", inversedBy="goods", fetch="EAGER")
* @JoinColumn(name="id_shop", referencedColumnName="id")
**/
public $shop;
/** @Column */
public $name;
}
$shopRepository = $entityManager->getRepository('\Shop');
$shops = $shopRepository->findAll();
echo "\n";
//print_r($shops);
foreach ($shops as $shop) {
//$shop->goods = null;
//print_r($shop);
echo $shop->id."-";
echo count($shop->goods)."\n";
foreach ($shop->goods as $goods) {
echo $goods->name.";";
}
echo "\n";
}
foreach ($logger->queries as $q) {
echo SqlFormatter::format($q['sql'])."
\r\n";
}
?>





0 Comments