0

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.

Mentos93
  • 581
  • 1
  • 7
  • 28

1 Answers1

0

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'

Community
  • 1
  • 1
Mawcel
  • 1,967
  • 15
  • 22