Can't pass value of parameter into findOneBy S

2019-02-28 02:18发布

I am using Symfony (version 2.5.0-DEV) and the doctrine mongodb cookbook on http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html.

I am stuck at the moment trying to pass the value of a defined parameter into findOneByIndex.

So if I do the following $script = $repository->findOneByIndex(1); it works perfectly. But if I do the following $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:

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

Any help would be appreciated :).

5条回答
太酷不给撩
2楼-- · 2019-02-28 03:03

Using the echo function, I can see that it works when value is simply entered (not parameter). 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();

   }
}
?>
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-28 03:07

Thanks jmikola. the {user} is more or less a parameter and it carries a value. 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). The issue is that passing the $user to the controller and using the findOneBy only works with Id not with index or 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;
}
}
查看更多
老娘就宠你
4楼-- · 2019-02-28 03:14

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.

$repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
$script = $repository->findOneByIndex($user->getId());
查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-28 03:16

In your last code example, what is the type of the $user variable? I assume it may be a string if it's a routing parameter and coming from the URI. You can use var_dump() to get the type and value in one shot.

Based on an earlier comment, you said that the Scripts document had the following fields:

  • _id
  • name (string)
  • description (string)
  • index (integer)
  • user_id (integer)

If the index field in your MongoDB document is an integer, you will need to use an integer in the query. For instance, the findOneByIndex('1') will not match a document with integer 1 in its field. 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. 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. You said that this failed to find a result. I assume the $user argument to your controller is a user ID. If that is the case, why were you querying on the index field instead of user_id?

查看更多
倾城 Initia
6楼-- · 2019-02-28 03:22

Do you pass $user to showuser action? like this (notice $user as parameter):

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

PS. I'd recommend you to change var $user to $userId just not to mistake it with $user object

查看更多
登录 后发表回答