跳至主要内容

博文

目前显示的是标签为“symfony2-doctrine2”的博文

doctrine left inner join /left outer join

RIGHT, INNER, OUTER, JOIN? The difference is in the way tables are joined if there are no common records. JOIN is same as INNER JOIN and means to only show records common to both tables. Whether the records are common is determined by the fields in join clause. For example: FROM t1 JOIN t2 on t1.ID = t2.ID means show only records where the same ID value exists in both tables. LEFT JOIN is same as LEFT OUTER JOIN and means to show all records from left table (i.e. the one that precedes in SQL statement) regardless of the existance of matching records in the right table. RIGHT JOIN is same as RIGHT OUTER JOIN and means opposite of LEFT JOIN, i.e. shows all records from the second (right) table and only matching records from first (left) table.  也就是说: left outer join:不管右边有没有,左边的都选上 right outer join : 不管左边的有没有,右边的都悬赏 join/inner join : 必须两遍都有

symfony2 SQLLoger memory leak problem

recently i have problem when i trying to import a large text file(about 80M)into database by Doctrine and symfony2. Well, i always got a "out of memory" exception. After some searching job, i found this: Commands are run in prod env by default but have debug enabled, it's  different than the app.php that is prod but where debug is  disabled ...  So using "php app/console xxx:xxx --no-debug" fixed the leaks because  there was no more SQLLogger.  An other solution was to use inside my Command:  $configuration = $this->em->getConnection()-> getConfiguration();  $configuration->setSQLLogger( null);  and the answer is this; Disabling the logger is the only solution. its probably not very useful to have the logger enabled in console aswell, but i dont see an immediate solution to fixing this. You can of course do $em->getconfiguration()-> setSQLLogger(null); in the command So well there is no way to do it in a de...

mysql groupby and having

SELECT words , COUNT (*) AS cnt FROM words GROUP BY word HAVING COUNT (*) > 1 by doctrine: $qb = $this->getEntityManager()->createQueryBuilder(); $qb ->select(array( 'u.id', 'u.name', 'u.reseller_code', 'u.first_name', 'u.last_name', 'u.email', 'u.country', 'COUNT(a) as numam', 'SUM(IF(a.activity_year = :year, a.activity_value, 0)) as sumay', 'SUM(IF(a.activity_year = :year AND a.activity_month = :month, a.activity_value, 0)) as sumam' )) ->from('MyBundle:User', 'u') ->leftJoin('u.activities', 'a') ->groupBy('u.id') ->orderBy('u.name', 'ASC') ->setParameter('month', $month) ->setParameter('year', $year)

php doctrine findby

sometimes it will take a very time to run, i got this example: public function receiveAction() { $originData=$this->get('request')->request->get('data'); if (!isset($originData)){ $response = new Response(json_encode(array('msg' => 'error,no data'))); $response->headers->set('Content-Type', 'application/json'); return $response; } $data = json_decode( $originData ); if (!is_array($data)){ $response = new Response(json_encode(array('msg' => 'error,data is not correct:'.$originData))); $response->headers->set('Content-Type', 'application/json'); return $response; } $em=$this->getDoctrine()->getEntityManager('webscraper'); foreach($data as $row){ if (!isset($row[0])) continue; /* $ad=$em->getRepository('AcmeDataCollectionBundle:SmallAdds...

symfony prod debug=true , get blank page.

i have a symfony2 project works well, after some modification of html, i always get a blank page when i set in app.php: $kernel = new AppKernel('prod', false); but if i set the debug parameter to true: $kernel = new AppKernel('prod', true); everything works fine, i haven't see any log in my prod.log. I use php apc and fastCGI on my server. Anyone can tell me the difference between the parameter? Or this is just a problem of cache? finally, i have found the problem. I searched again in my log of appache, i got this error:  [Fri Oct 26 14:12:19 2012] [error] [client 127.0.0.1] PHP Fatal error: require() [function.require]: Failed opening required 'D:/workspace/symfony_bannieres/app/cache/devprod/doctrine/orm/Proxies\AcmeModerationBundleEntityModerationObjectStatusProxy.php' (include_path='.;d:\software\wamp\bin\php\php5.3.9\pear') in D:\workspace\symfony_bannieres\vendor\doctrine\lib\Doctrine\ORM\Proxy\ProxyFactory.php on line 85 ...

find and findby

"find" works only for a single id. If you want to query for a set of id's you can use "findBy" $em = $this->getDoctrine()->getEntityManager(); $em->getRepository('AcmeUserBundle:User').find($id); $em->getRepository('AcmeBannieresBundle:BannerCity')->findBy(array('id' =>$banners));

doctrine 一次insert 或者操作大量 record

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/batch-processing.html#batch-processing 我的例子: $cities=Array(); $cities=Array(); $em = $this->getDoctrine()->getEntityManager(); $qb=$em->getRepository('AcmeBannieresBundle:TVille')->createQueryBuilder('tv'); //$cities=$qb->getQuery()->execute(); $iterableResult =$qb->getQuery()->iterate(); $j=0; while (($row = $iterableResult->next()) !== false) { $city=$row[0]; //$city=new TVille(); for ($p=1;$p++;$p setCity($city); $banner->setPosition($p); //$city->addBannerCity($banner); $em->persist($banner); } //$em->persist($city); $j++; if ($j>100){ $em->flush(); $em->clear(); // Detaches all objects from Doctrine! $j=0; } ...

one to many 或者many to many 的 eager fetch

如果你想不要让doctrine 为每一个related one to many 或者many to many 生成一个sql的话,只能直接写DQL 比如: $qb=$em->getRepository('AcmeBannieresBundle:TVille')->createQueryBuilder('tv'); $qb->leftJoin('tv.codep', 'cp') ->leftJoin('tv.departement', 'dep') ->where('dep.code = :codep') ->setParameter('codep', $codep); $cities=$qb->getQuery()->execute(); 是不行的,每次你要读codep的时候它都要query一把,dql可以解决这一点: $query=$em->createQuery('SELECT tv,cp FROM AcmeBannieresBundle:TVille tv LEFT JOIN tv.codep cp LEFT JOIN tv.departement dep WHERE dep.code= :codep') ->setParameter('codep', $codep); $cities=$query->getResult(); 这个就只执行一个sql。

doctrine2 where in的问题

$queryBuilder->select('o')-> from('Object', 'o')->where($queryBuilder-> expr()->in(' o.id ', $ids)); Where $ids = array(1, 2, 5, 6, ...). This works very well. On the other hand, the following equivalent DQL yields only 1 result: whichever id is the first in the $ids list.  $this->getEntityManager()-> createQuery('SELECT o FROM Object o '.       'WHERE  o.id  IN (:ids) ')       ->setParameter('ids', implode(', ', $ids));