1

In my symfony project I have an array $productContainer with some values like (php dump):

array:2 [▼
  0 => "value1"
  1 => "value2"
]

I pass this array in the Product entity by findBy method in my controller like this:

$products = $this->getDoctrine()->getRepository('MyBundle:Product')
                                ->findByValue($productContainer);

The results between the findBy method and the array values match very well.

But when I check if the array is an instance of my class Product like this:

dump($products instanceof Product);
die;

it retuns me : false

I understand that $products is an array and not an object, but how can I declare my array $products as an instanceof Product entity ?

Edit

In order to be more precise, I need to declare or check if my values of array $products are instanceof Product because in the same controller, I have to pass array $products in a queryBuilder for another entity like this:

$entity = $this->getDoctrine()->getRepository('MyBundle:Entity')
                              ->getEntityWithProducts($slug, $products);

I recover the array $products by a $_POST method (Request $request) Its a controller method that I retun into JsonResponse, that why I proceed in that way.

9
  • Class instances are always objects. Commented Jan 27, 2016 at 11:30
  • 1
    This could not be reached, array would never be instance of class. Tell me, please, why do you need this? Commented Jan 27, 2016 at 11:31
  • 2
    @VladimirKovpak, he would never receive clear instance of Product entity! It is because he uses findBy method - it never returns one object, it returns either array of objects (even if there is only one) or null. He need either to check each array member for instanceof or don't need to check any of them. Commented Jan 27, 2016 at 11:58
  • 1
    No problem! And I suggest you to add a little modification into your answer: If he still wants to fetch an array of records through his query, the most simple and the only one right (imho!) variant is to check whether the result array is empty or not - if it is empty, nothing found, if not - there are some records and all(!) of them are instances of his Product entity. :) Commented Jan 27, 2016 at 12:13
  • 1
    @VladimirKovpak, it is not unclear, it's eh-h-hm... Let's agree that it is strange to not offend anyone :) Commented Jan 27, 2016 at 12:27

2 Answers 2

3

Try findOneByValue instead findByValue if you need just one product.
Or extract some one element from your received array, because you receive array of entities after invoking findByValue.
Or traverse all elements in received array to check them.
Or maybe in your product repository present method findByValue that do some staff for you.

But anyway it sounds strange to check after doctrine that it returns for you appropriate class instance...
If you use something like getArrayResult you'll receive array, otherwise you'll receive instance of appropriate entity.

Sign up to request clarification or add additional context in comments.

Comments

1

Arrays can't be of some object class, but if I understand what you want to do, maybe you can try using array functions.

Try this

$test = array_reduce(
          $products, 
          function ($condition, $item) {
            return $condition && $item instanceof Product;
          }, 
          true
        );
// $test will be true if all elements of $products are instances 
// of 'Product', false otherwise

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.