Ok I am gonna have to dig into this when I get a moments chance cos that should have worked.
Ok I am gonna have to dig into this when I get a moments chance cos that should have worked.
Sorry for the lateness but I did finally get round to test this and it works for me.
Using the latest version of MongoYii I used this in the siteController in the test repository:
$u=new User();
$u->username='gjgjgjg';
$u->email='sdfdfdsfdsfdsf';
$u->password='dffdsfsf';
$u->ex_sub['title'] = 'sub ';
$u->validate();
var_dump($u);
with this in the model:
array('ex_sub', 'subdocument', 'type' => 'one', 'rules' => array(
array('title', 'filter', 'filter' => 'trim')
)),
And after validation I get this value for the subdocument title field:
public 'ex_sub' =>
array
'title' => string 'sub' (length=3)
As you can see it has been trimmed.
I decided to take it a step further and add this to the model:
array('ex_sub', 'subdocument', 'type' => 'one', 'rules' => array(
array('title', 'filter', 'filter' => function($c){
return new MongoId();
})
)),
What I get in the var_dump is:
public 'ex_sub' =>
array
'title' =>
object(MongoId)[30]
public '$id' => string '52b5c1f06803fafb06f2cef2' (length=24)
So this is working perfectly for me.
It should be noted that the line that produces these var_dumps is of course the one in the sitecontroller var_dumping the user model after validation.
Can you show some var_dumps and that?
I should note that because I have actually tested the fix I did as working now I have pushed it to the 3.1.5 tag on GitHub
Thank you for looking into this problem. I’ll try again on Monday, when I’m at work. It seems it should have worked for me as well. Sorry if I bothered you for nothing.
Its ok, better to get the help and be sure something works than sit there endless wondering
Hi all,
I’m having great difficulty getting a search() function to work.
The index.php works fine but the $model->search() call in admin.php is returning nothing. I’ve only one document in one collection in Mongo. My code is very similar to Vivi’s on the previous page so I’m puzzled about why it isn’t working.
Does anyone know where I’ve went wrong?
UPDATE: If I create the EMongoDataProvider with an empty $criteria array then I get data returned, but as expected the filter does not work. This is very strange…
return new EMongoDataProvider($this, array('criteria' => array(),));
Here is my code:
class MUser extends EMongoDocument {
...
public function rules()
{
return array(
array('username', 'required'),
array('username', 'length', 'max'=>20),
array('_id, username', 'safe', 'on'=>'search'),
);
}
...
public function search()
{
$criteria=new EMongoCriteria();
$criteria->compare('_id',$this->_id);
$criteria->compare('username',$this->username, true);
return new EMongoDataProvider($this, array(
'criteria'=>$criteria,
));
}
and controller
class MUserController extends Controller
...
public function actionIndex()
{
$dataProvider=new EMongoDataProvider('MUser');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
...
public function actionAdmin()
{
$model=new MUser('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['MUser'])) {
$model->attributes=$_GET['MUser'];
}
$this->render('admin',array(
'model'=>$model,
));
}
and index.php
$this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
));
and admin.php
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'_id',
'username',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
)); ?>
Thanks, ND.
Sorry for not replying in a timely manner, was very busy when I notified of this reply and the forum isn’t very good at reminding you of things.
Anyway, did you get this solved?
Ha, I have this problem too, it is not understanding that username is empty as such shouldn’t use it, seems like a quick fix
Ok I can’t fix this, instead you will need to work around it with:
if($this->username){
$criteria->compare('username', $this->username, true);
}
currently.
The reason for this is because I actually have a line in the compare function that does:
if($value === null){
$query[$column] = null;
Right at the beginning, this is a problem and Yii1 does not actually do this, it ignores null values but I added this and it (reportedly) is used in quite a few projects as such taking it out could…break a lot of things.
So currently, you need to check if something is set first.
Hi Yii Experts !
I am novice in YII and I have one question concerning great extension MongoYII
I set It up via main.php (config) and I can use it from my code like this
$cursor = Yii::app()->mongodb->MyCollection->find();
It works nice ) But sometimes my Mongo server down and I need to print message error so How can I set Exception to MongoYII ?
thanks
You can do you a exception catch since the driver will by default throw a connection exception there I think.
But for failover reasons you probably want to catch both connection and cursor exceptions since a cursor exception can be thrown halfway through reading the documents if your read preference cannot fulfill the query.
I suppose you could catch any: http://php.net/manual/en/class.mongoexception.php and deal with that but it might not be the best idea it might be better to catch the exceptions individually.
You are right with above notice
but in YII I included/setup MongoYII extension via config file (main.php) like this
'application.extensions.MongoYii.*',
'application.extensions.MongoYii.validators.*',
'application.extensions.MongoYii.behaviors.*',
'application.extensions.MongoYii.util.*'
and I may suppose that Exceptions should be set up in MomgoYii scripts at /extensions/ folder
What would be the scenario for abstracting the Exceptions into the extension?
I mean what benefits would it create?
Hi !
Honesty Above questions about "benefints" is not completely clear for me
But I try to reply on it.
The main benefit is ERROR/WARNING text being formated by me
For example I wanna show message "Connection to Db is impossible now" . Please try later"
(with red 1 pixed dashed border ) on unsuccessful connection to MongoDb instead of standart/default YII framwork message
if I use pure PHP I can catch ERROR on connection
try {
// Connecto to MongoDb
$conn = new Mongo('mongodb://localhost:27017');
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: '.$e->getMessage());
}
In case with using http://www.yiiframework.com/extension/mongoyii I cant because connection is done in config YII file main.php
like this
'application.extensions.MongoYii.*',
'application.extensions.MongoYii.validators.*',
'application.extensions.MongoYii.behaviors.*',
'application.extensions.MongoYii.util.*'
and in script I do query to MongoDb at once without connection like
$cursor = Yii::app()->mongodb->MyCollection->find();
so Is It possible to use try{}catch{} construction with MongoYII extention during Connection to database ?
Thanks
Ok I see the problem, the biggest thing stopping you here is that connect() (which does this) is called on init of the extension.
The only way I can make this open to you to is to make it so that you have to add a setting telling the extension you do not wish to connect immediately and you run connect() yourself in a try{} block.
What do you think?