Php 如何检查或声明数组的数组值,如类/实体的实例
在我的symfony项目中,我有一个数组Php 如何检查或声明数组的数组值,如类/实体的实例,php,arrays,symfony,doctrine-orm,instanceof,Php,Arrays,Symfony,Doctrine Orm,Instanceof,在我的symfony项目中,我有一个数组$productContainer,其中包含一些值,如(php dump): 我通过控制器中的findBy方法在Product实体中传递此数组,如下所示: $products = $this->getDoctrine()->getRepository('MyBundle:Product') ->findByValue($productContainer); dump($pr
$productContainer
,其中包含一些值,如(php dump):
我通过控制器中的findBy
方法在Product
实体中传递此数组,如下所示:
$products = $this->getDoctrine()->getRepository('MyBundle:Product')
->findByValue($productContainer);
dump($products instanceof Product);
die;
$entity = $this->getDoctrine()->getRepository('MyBundle:Entity')
->getEntityWithProducts($slug, $products);
findBy方法和数组值之间的结果非常匹配
但是当我检查数组是否是我的类产品
的一个实例时,如下所示:
$products = $this->getDoctrine()->getRepository('MyBundle:Product')
->findByValue($productContainer);
dump($products instanceof Product);
die;
$entity = $this->getDoctrine()->getRepository('MyBundle:Entity')
->getEntityWithProducts($slug, $products);
它再次提醒我:错误
我知道$products
是一个数组而不是对象,但是如何将我的数组$products
声明为产品
实体的实例
编辑
为了更精确,我需要声明或检查数组$products
的值是否为instanceof
Product
,因为在同一个控制器中,我必须在queryBuilder中为另一个实体传递数组$products,如下所示:
$products = $this->getDoctrine()->getRepository('MyBundle:Product')
->findByValue($productContainer);
dump($products instanceof Product);
die;
$entity = $this->getDoctrine()->getRepository('MyBundle:Entity')
->getEntityWithProducts($slug, $products);
我通过$\u POST
方法(Request$Request
)恢复阵列$products
)
这是我重新运行到JsonResponse中的一种控制器方法,这就是我以这种方式进行操作的原因。如果您只需要一个产品,请尝试findOneByValue
而不是findByValue
。
或
从接收到的数组中提取某个元素,
因为您在调用findByValue
后收到实体数组。
或
遍历接收数组中的所有元素以检查它们。
或
也许在您的产品存储库中提供了方法findByValue
,可以为您提供一些工作人员
但不管怎样,检查它是否为您返回合适的类实例听起来很奇怪。。。
如果您使用类似于getArrayResult
的东西,您将收到数组,否则您将收到相应实体的实例。如果您只需要一个产品,请尝试findOneByValue
而不是findByValue
。
或
从接收到的数组中提取某个元素,
因为您在调用findByValue
后收到实体数组。
或
遍历接收数组中的所有元素以检查它们。
或
也许在您的产品存储库中提供了方法findByValue
,可以为您提供一些工作人员
但不管怎样,检查它是否为您返回合适的类实例听起来很奇怪。。。
如果您使用类似于getArrayResult
的东西,您将接收数组,否则您将接收相应实体的实例。数组不能属于某个对象类,但如果我了解您想要做什么,也许您可以尝试使用数组函数
试试这个
$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
数组不能属于某个对象类,但如果我理解您想要做什么,也许您可以尝试使用数组函数
试试这个
$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
类实例始终是对象。无法访问此对象,数组将永远不会是类的实例。请告诉我,你为什么需要这个?@VladimirKovpak,他永远不会收到产品
实体的清晰实例!这是因为他使用了findBy
方法-它从不返回一个对象,它返回对象数组(即使只有一个)或null。他需要检查每个数组成员的instanceof
,或者不需要检查任何一个。没问题!我建议你在答案中添加一点修改:如果他仍然想通过他的查询获取一个记录数组,最简单也是唯一正确的(imho!)变体是检查结果数组是否为空-如果为空,则未找到任何内容,如果不为空,则存在一些记录和所有(!)其中包括his产品
实体的实例:@弗拉基米尔科帕克,还不清楚,是呃,嗯。。。让我们同意不冒犯任何人是很奇怪的:)类实例总是对象。这是无法达到的,数组永远不会是类的实例。请告诉我,你为什么需要这个?@VladimirKovpak,他永远不会收到产品
实体的清晰实例!这是因为他使用了findBy
方法-它从不返回一个对象,它返回对象数组(即使只有一个)或null。他需要检查每个数组成员的instanceof
,或者不需要检查任何一个。没问题!我建议你在答案中添加一点修改:如果他仍然想通过他的查询获取一个记录数组,最简单也是唯一正确的(imho!)变体是检查结果数组是否为空-如果为空,则未找到任何内容,如果不为空,则存在一些记录和所有(!)其中包括his产品
实体的实例:@弗拉基米尔科帕克,还不清楚,是呃,嗯。。。让我们同意不冒犯任何人是很奇怪的:)