按字段OneToMany

时间:2015-12-03 13:50:52

标签: php symfony doctrine-orm twig

谁知道如何解决这个简单的问题? 我有一个实体项目,字段likeUsers和twig统计这个字段,但我希望通过这个计数的订单项目(DESC) - 首先谁有更多preferUsers。怎么做?在查询构建器或twig中创建过滤器?帮助学说我知道计数

"likes_user" => count($this->getLikedUsers()->getValues())

如何从这个字段中对我的所有项目进行排序?

或者它是如何用查询构建器解决的? 类似的东西,但这不起作用:

public function getProjects()
{
    $qb = $this->getEntityManager()->createQueryBuilder('d');
    $qb
        ->select('d')
        ->from('AppBundle:Project', 'd')
        ->where('d.confirm = :identifier')
        ->setParameter('identifier', 'approved')
        ->orderBy('COUNT(d.likedUsers)', 'DESC')
        ->getQuery()
        ->getResult()
    ;
    $query = $qb->getQuery();
    $results = $query->getResult();
    return $results;
}

实体:

/**
 * Project
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ProjectRepository")
 */
class Project implements \JsonSerializable
{
    /**
     * @var Collection
     *
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="likedProjects")
     */
    private $likedUsers;




{% for project in projects %}
<div>LIked: <span>{{ project.likedUsers|length|number_format(0, '.', ' ') }}</span></div>
{% endfor %}

我想也许是这样的:

{% for project in projects.likedUsers|length|sort %}

但不影响
也许谁知道?数组项目如何按字段LikedUser的数量排序。我不知道怎么做。查询构建器或树枝扩展或usort ..

2 个答案:

答案 0 :(得分:0)

恕我直言,这似乎是2015-12-03 07:46:44,588 DEBUG: [SqlExceptionHelper] - SQL Warning com.ibm.db2.jcc.am.SqlWarning: Origination unknown: [10228][11541][4.16.53] Secu rity exceptions occurred while loading driver. ERRORCODE=4223, SQLSTATE=null at com.ibm.db2.jcc.am.fd.b(fd.java:207) at com.ibm.db2.jcc.am.fd.b(fd.java:258) at com.ibm.db2.jcc.am.hb.a(hb.java:1326) at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:710) at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:687) at com.ibm.db2.jcc.t4.b.<init>(b.java:334) at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233) at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199) at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:474) at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:115) at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:307) at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200) at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:708) at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:642) at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:464) at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:141) at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115) at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102) at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1048) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:825) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689) at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) at com.comerica.tir.monitor.app.MonitorApplication.main(MonitorApplication.java:24)Caused by: java.security.PrivilegedActionException: java.lang.ClassNotFoundException: sun.io.ByteToCharConverter at java.security.AccessController.doPrivileged(Native Method) at com.ibm.db2.jcc.am.hb.x(hb.java:1312) at com.ibm.db2.jcc.am.hb.<clinit>(hb.java:659) at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:125) ... 40 more Caused by: java.lang.ClassNotFoundException: sun.io.ByteToCharConverter at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.ibm.db2.jcc.am.mg.run(mg.java:19) ... 44 more 而不是PHP的工作。不确定Doctrine是否可以实现,如果可能的话,会产生什么影响。

执行此操作的一种方法是MVC (Model, View Control)

Doctrine

$projects = ...; usort($projects, function($p1, $p2){ // Both $p1 and $p2 are instance of Project // Assuming that `getLikedUsers()` return Doctrine `Collection`... return count($p1->getLikedUsers()) > count($p2->getLikedUsers()); }); 获取一个数组,并在每个传递中提供两个项目(在您的情况下为两个项目),供您决定哪个是&#34;更大&#34;。在你的情况下,有更多喜欢的项目肯定是&#34;更大&#34;,对吧?

希望这会有所帮助......

答案 1 :(得分:0)

我用查询构建器解决了。而且我只添加了喜欢用户的项目 - 用户住在某个城市没有所有城市

public function getProject()
{
    $qb = $this->getEntityManager()->createQueryBuilder('d');
    $qb
        ->select('d')
        ->from('AppBundle:Project', 'd')
        ->addSelect('COUNT(m.id) as nMethods')
        ->join('d.likedUsers', 'm')
        ->join('m.location', 'l')
        ->where('l.city = :identifier')
        ->setParameter('identifier', 'someCity')
        ->groupBy('d.id')
        ->orderBy("nMethods", 'DESC')

        ->getQuery()
        ->getResult()
    ;
    $query = $qb->getQuery();
    $results = $query->getResult();
    return $results;
}