Reputation: 2177
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 ?
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.
Upvotes: 1
Views: 352
Reputation: 714
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
Upvotes: 1
Reputation: 17071
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.
Upvotes: 3