2014-01-13 31 views
1

我正在使用Symfony(版本2.5.0-DEV)和原理mongodb食譜http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html無法將參數值傳遞到findOneBy Symfony存儲庫

我被困在試圖將定義參數的值傳遞給findOneByIndex的那一刻。

所以,如果我做了以下$script = $repository->findOneByIndex(1);它完美的作品。 但是,如果我做了以下$script = $repository->findOneByIndex($user);它無法搜索與用戶的值。

在routing.yml中,我有這個pattern: platform/designing/users/{user}/showuser,並在控制器我只是做了showuserAction下:

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

任何幫助,將不勝感激:)。

回答

1

該值應該是一個整數,而不是對象。請傳遞id或索引值 - 我不知道它是如何存儲在mongodb中的。

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

感謝您的答覆。索引是一個整數值。我用你的命令並得到:FatalErrorException:錯誤:調用成員函數getId() – Kingsley

+0

哪個屬性是您的實體的索引? –

+0

對不起,我不明白你的問題。也許我可以說明我的字段或列(MongoDB):_id,name,description,index,user_id。 index和user_id是整數,但是_id不能被編輯(自動生成) – Kingsley

0

您是否通過$usershowuser的操作?像這樣(注意$ user作爲參數):

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

PS。我建議你將var $user更改爲$userId只是不要把它誤認爲$ user對象

+0

是的,我在路由下的showuser操作中傳遞了用戶:pattern:metaplatform/designing/users/{user}/showuserjson。並在控制器中:public function showuserAction($ user)。我也可以在頁面上看到echo的用戶值。但我無法通過查找。 – Kingsley

+0

我也將var $ user更改爲$ userId,但我得到的結果相同。 – Kingsley

0

使用echo函數,我可以看到它在僅輸入值(而不是參數)時起作用。請參閱下面的完整控制器腳本。任何幫助,將不勝感激:)

<?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(); 

    } 
} 
?> 
+0

'echo'是不夠的,因爲它會隱藏變量的類型。這就是爲什麼我在[先前的回答](http://stackoverflow.com/a/21122162/162228)中詢問您使用'var_dump()',並且在[重複問題](http://stackoverflow.com/q/21138334/162228)你在這個問題上打開。基於迄今爲止提供的所有內容,這看起來非常像打字問題。儘管你可能習慣使用PHP,但在MongoDB中,字符串「」1「將**不匹配數字」1「。有關更多信息,請參閱[此處](http://docs.mongodb.org/manual/faq/developers/#what-is-the-compare-order-for-bson-types)。 – jmikola

2

在你最後的代碼示例,什麼是$user變量的類型?我假設它可能是一個字符串,如果它是一個路由參數並來自URI。您可以使用var_dump()來一次獲得類型和數值。

基於以前的評論,你說的那個腳本文件有以下字段:

  • _id
  • 名稱(字符串)
  • 描述(字符串)
  • 指數(整數)
  • user_id(整數)

如果您的MongoDB文檔中的字段是整數,您需要在查詢中使用整數。例如,findOneByIndex('1')將與其字段中整數爲1的文檔不匹配。這裏的最佳做法是在查詢之前將您的值設置爲適當的類型。最好停止依賴神奇的DocumentRepository方法,並明確定義自己的內部執行的方法。然後,您的控制器可以直接從路由或請求參數傳遞數字字符串,而不必擔心自己執行整型投射。

此外,爲您的原始代碼示例評論:

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

這是爲路由模式platform/designing/users/{user}/showuser。你說這沒有找到結果。我假設你的控制器的$user參數是一個用戶ID。如果是這樣的話,你爲什麼要詢問index而不是user_id

0

謝謝jmikola。 {user}或多或少是一個參數,它帶有一個值。我查詢要測試的索引以及user_id,但它們都不起作用(使用索引進行測試,因爲它與user_id具有相同的整數,並避免user_id中的下劃線)。問題是將$用戶傳遞給控制器​​,並且使用findOneBy僅適用於Id,不適用於index或user_id。那就是:

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

我的文檔腳本粘貼如下:

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; 
} 
}