dongli564510 2017-03-27 13:04
浏览 48

从对象获取子对象并创建尚不存在的新对象

I'm trying to get three properties from all the child objects that belongs to the parent object in Sonata Admin.

I have my parent object;

$parent = $this->admin->getSubject();

Then I want to get all the child objects from the parent object like this;

$children = $parent->getChildObjects;

How do I then create one array of three properties (all three properties are integers) from all the children objects together?

For example;

array(3) {
  [0]=> '1-1-1'
  [1]=> '1-1-2'
  [2]=> '1-1-3'
  [3]=> '1-2-1'
  [4]=> '1-2-2'
  [5]=> '1-2-3'
  [6]=> '1-3-1'
  [7]=> '1-3-2'
  [8]=> '1-3-3'
  [9]=> '2-1-1'
  [10]=> '2-1-2'
  [11]=> '2-1-3'
  [12]=> '2-2-1'
  [13]=> '2-2-2'
  [14]=> '2-2-3'
  [15]=> '2-3-1'
  [16]=> '2-3-2'
  [17]=> '2-3-3'
  [18]=> '3-1-1'
  [19]=> '3-1-2'
  [20]=> '3-1-3'
  [21]=> '3-2-1'
  [22]=> '3-2-2'
  [23]=> '3-2-3'
  [24]=> '3-3-1'
  [25]=> '3-3-2'
  [26]=> '3-3-3'
}

Because what I want to do is, I want to create more children objects but it has to check in that array if that child does not yet exist in combination of the three propeties. If it does exist it should skip that number and try the next one in the loop.

How can I accomplish this? I'm fairly new with programming, and I only have a little bit of symfony and sonata knowledge.

If anybody has other ideas with other kind of array (multidimensional) for example, I stand open for different ideas.

  • 写回答

1条回答 默认 最新

  • douchuang8359 2017-03-28 08:37
    关注

    Here is one way to build an array with the three properties :

    $children = [];
    
    foreach( $parent->getChildren() as $child ){
        $children[] = [
             'property1' => $child->getProp1(), 
             'property2' => $child->getProp2(),
             'property3' => $child->getProp3()
        ];
    }
    

    Then you can use a repository method like this one to make your query:

        public function findByNot( array $criteria, array $orderBy = null, $limit = null, $offset = null )
        {
            $qb = $this->getEntityManager()->createQueryBuilder();
            $expr = $this->getEntityManager()->getExpressionBuilder();
    
            $qb->select( 'entity' )
                ->from( $this->getEntityName(), 'entity' );
    
            foreach ( $criteria as $field => $value ) {
    
                $qb->andWhere( $expr->neq( 'entity.' . $field, $value ) );
            }
    
            if ( $orderBy ) {
    
                foreach ( $orderBy as $field => $order ) {
    
                    $qb->addOrderBy( 'entity.' . $field, $order );
                }
            }
    
            if ( $limit )
                $qb->setMaxResults( $limit );
    
            if ( $offset )
                $qb->setFirstResult( $offset );
    
            return $qb->getQuery()
                ->getResult();
        }
    

    Doctrine findBy 'does not equal'

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题