2017-03-27 12 views
0

Sonata Adminの親オブジェクトに属するすべての子オブジェクトから3つのプロパティを取得しようとしています。オブジェクトから子オブジェクトを取得し、まだ存在しない新しいオブジェクトを作成する

私は親オブジェクトを持っています。

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

それから私はこのような親オブジェクトからすべての子オブジェクトを取得したいです。

$children = $parent->getChildObjects;

どのように私はその後、一緒にすべての子供たちからの3つのプロパティ(すべての3つのプロパティである整数)のいずれかの配列をオブジェクト作成するのですか?

たとえば、

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' 
} 

私がやりたいことは、私はより多くの子供オブジェクトを作成したいが、その子はまだ3 propetiesの組み合わせに存在しない場合、それはその配列にチェックする必要があるため。存在する場合は、その番号をスキップして、ループ内の次の番号を試してください。

どうすればこの問題を解決できますか?私はプログラミングに関してかなり新しいです、そして、私は少しのsymfonyとソナタ知識しか持っていません。

誰かが他の種類の配列(多次元)を持つ他のアイデアを持っている場合、私はさまざまなアイディアのためにオープンしています。ここで

答えて

0

3つのプロパティを持つ配列を構築するための一つの方法である:

$children = []; 

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

次に、あなたがあなたのクエリを作るために、このようなリポジトリの方法を使用することができます。

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'

関連する問題