简体   繁体   English

无法将参数值传递到findOneBy Symfony存储库中

[英]Can't pass value of parameter into findOneBy Symfony repository

I am using Symfony (version 2.5.0-DEV) and the doctrine mongodb cookbook on http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html . 我正在使用Symfony(版本2.5.0-DEV)和http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html上的mongodb烹饪手册。

I am stuck at the moment trying to pass the value of a defined parameter into findOneByIndex. 我现在正试图将已定义参数的值传递给findOneByIndex。

So if I do the following $script = $repository->findOneByIndex(1); 因此,如果我执行以下$script = $repository->findOneByIndex(1); it works perfectly. 它运作完美。 But if I do the following $script = $repository->findOneByIndex($user); 但是如果我执行以下$script = $repository->findOneByIndex($user); it fails to search with the value of user. 无法使用用户的值进行搜索。

In routing.yml, I have this pattern: platform/designing/users/{user}/showuser and in the controller I simply do this under the showuserAction: 在routing.yml中,我有以下pattern: platform/designing/users/{user}/showuser ,在控制器中,我只是在showuserAction下执行此操作:

$repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
$script = $repository->findOneByIndex($user);

Any help would be appreciated :). 任何帮助,将不胜感激 :)。

In your last code example, what is the type of the $user variable? 在您的最后一个代码示例中, $user变量的类型是什么? I assume it may be a string if it's a routing parameter and coming from the URI. 我认为如果它是一个路由参数并且来自URI,则可能是字符串。 You can use var_dump() to get the type and value in one shot. 您可以使用var_dump()一次性获得类型和值。

Based on an earlier comment, you said that the Scripts document had the following fields: 根据先前的评论,您说脚本文档具有以下字段:

  • _id _ID
  • name (string) 名称(字符串)
  • description (string) 说明(字符串)
  • index (integer) 索引(整数)
  • user_id (integer) user_id(整数)

If the index field in your MongoDB document is an integer, you will need to use an integer in the query. 如果MongoDB文档中的index字段是整数,则需要在查询中使用整数。 For instance, the findOneByIndex('1') will not match a document with integer 1 in its field. 例如, findOneByIndex('1')将与字段中的整数为1的文档不匹配。 A best practice here is to cast your values to the appropriate type before querying. 这里的一个最好的做法是在查询之前你的价值观,以适当的类型。 It may also be best to stop relying on the magic DocumentRepository methods and explicitly define your own findBy methods, which do the casting internally. 最好也不再依赖神奇的DocumentRepository方法,并显式定义自己的findBy方法,这些方法在内部进行转换。 Then, your controller can pass a numeric string directly from a routing or request parameter and not have to worry about doing the integer cast on its own. 然后,您的控制器可以直接从路由或请求参数传递数字字符串,而不必担心自己进行整数转换。

Also, to comment on your original code example: 另外,要评论您的原始代码示例:

$script = $repository->findOneByIndex($user);

This was for the routing pattern platform/designing/users/{user}/showuser . 这用于platform/designing/users/{user}/showuser的路由模式。 You said that this failed to find a result. 您说这找不到结果。 I assume the $user argument to your controller is a user ID. 我假设控制器的$user参数是一个用户ID。 If that is the case, why were you querying on the index field instead of user_id ? 如果是这样,为什么要在index字段而不是user_id上进行查询?

This value should be an integer, not object. 此值应为整数,而不是对象。 Please pass the id, or index value - I don't know how it's stored in mongodb. 请传递id或索引值-我不知道它在mongodb中的存储方式。

$repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
$script = $repository->findOneByIndex($user->getId());

Do you pass $user to showuser action? 您是否将$user传递给showuser动作? like this (notice $user as parameter): 像这样(注意$ user作为参数):

    public function showuserAction($user)
    {
        $repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
        $script = $repository->findOneByIndex($user);
    }

PS. PS。 I'd recommend you to change var $user to $userId just not to mistake it with $user object 我建议您将var $user更改为$userId只是不要与$ user对象$userId

Using the echo function, I can see that it works when value is simply entered (not parameter). 使用echo函数,可以看到仅输入值(而不是参数)时它可以工作。 Please see full controller script below. 请在下面查看完整的控制器脚本。 Any help would be appreciated :) 任何帮助,将不胜感激 :)

    <?php
// src/My/MpBundle/Controller/UserController.php
namespace My\MpBundle\Controller;

use Atlas\MpBundle\Document\Scripts;
use Symfony\Component\HttpFoundation\Response;//Write the html inside controller
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class UserController extends Controller
{
   public function showuserAction($userId)
   {
    //creating the repository below to help fetch objects
    $repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
//below method queries based on the user_id column.
$script = $repository->findOneBy(array('user_id' => $userId));

if (!$script) {
    throw $this->createNotFoundException('No product found for user with id '.$userId);
}

echo $script->getuserid();

   }
}
?>

Thanks jmikola. 谢谢jmikola。 the {user} is more or less a parameter and it carries a value. {user}或多或少是一个参数,并且带有一个值。 I query on the index to test as well as the user_id too but none of them works (using index to test as it has same integer as user_id and to avoid underscore in user_id). 我也查询索引以及user_id来进行测试,但是它们都不起作用(使用索引进行测试,因为它与user_id具有相同的整数,并避免在user_id中使用下划线)。 The issue is that passing the $user to the controller and using the findOneBy only works with Id not with index or user_id. 问题在于,将$ user传递给控制器​​并使用findOneBy仅适用于ID,不适用于index或user_id。 That is: 那是:

findOneById($userId);   //works
findOneByIndex($userId);   //fails
findOneByUserId($userId);   //fails

My document script is pasted below: 我的文档脚本粘贴在下面:

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;

 /**
 * @MongoDB\Document
 */

class scripts
{
    /**
     * @MongoDB\Id
     */
public $id;

/**
 * @MongoDB\String
 */
public $name;

/**
 * @MongoDB\String
 */
public $description;

/**
 * @MongoDB\String
 */
public $group;

/**
 * @MongoDB\Int
 */
public $index;

/**
 * @MongoDB\Boolean
 */
public $closed;

/**
 * @MongoDB\Int
 */
public $user_id;

/**
 * @MongoDB\String
 */
public $appearance;

/**
 * Get id
 *
 * @return id $id
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return self
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}

/**
 * Get name
 *
 * @return string $name
 */
public function getName()
{
    return $this->name;
}

/**
 * Set description
 *
 * @param string $description
 * @return self
 */
public function setDescription($description)
{
    $this->description = $description;
    return $this;
}

/**
 * Get description
 *
 * @return string $description
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Set group
 *
 * @param string $group
 * @return self
 */
public function setGroup($group)
{
    $this->group = $group;
    return $this;
}

/**
 * Get group
 *
 * @return string $group
 */
public function getGroup()
{
    return $this->group;
}

/**
 * Set index
 *
 * @param int $index
 * @return self
 */
public function setIndex($index)
{
    $this->index = $index;
    return $this;
}

/**
 * Get index
 *
 * @return int $index
 */
public function getIndex()
{
    return $this->index;
}

/**
 * Set closed
 *
 * @param boolean $closed
 * @return self
 */
public function setClosed($closed)
{
    $this->closed = $closed;
    return $this;
}

/**
 * Get closed
 *
 * @return boolean $closed
 */
public function getClosed()
{
    return $this->closed;
}

/**
 * Set userId
 *
 * @param int $userId
 * @return self
 */
public function setUserId($userId)
{
    $this->user_id = $userId;
    return $this;
}

/**
 * Get userId
 *
 * @return int $userId
 */
public function getUserId()
{
    return $this->user_id;
}

/**
 * Set appearance
 *
 * @param string $appearance
 * @return self
 */
public function setAppearance($appearance)
{
    $this->appearance = $appearance;
    return $this;
}

/**
 * Get appearance
 *
 * @return string $appearance
 */
public function getAppearance()
{
    return $this->appearance;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM