Yii Framework Forum: [Extension] Mongoyii - Yii Framework Forum

Jump to content

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6
  • You cannot start a new topic
  • You cannot reply to this topic

[Extension] Mongoyii Rate Topic: -----

#101 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 20 December 2013 - 04:45 AM

Ok I am gonna have to dig into this when I get a moments chance cos that should have worked.
0

#102 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 21 December 2013 - 11:32 AM

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?
0

#103 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 21 December 2013 - 11:38 AM

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
0

#104 User is offline   elke 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 26-July 13

Posted 21 December 2013 - 04:27 PM

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.
0

#105 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 21 December 2013 - 04:28 PM

Its ok, better to get the help and be sure something works than sit there endless wondering :)
0

#106 User is offline   NetDabbler 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-January 12

Posted 09 June 2014 - 10:24 AM

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.
0

#107 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 12 June 2014 - 03:25 AM

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?
0

#108 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 12 June 2014 - 03:56 AM

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
0

#109 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 12 June 2014 - 04:01 AM

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.
0

#110 User is offline   mrPascal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-August 14

Posted 08 August 2014 - 10:33 AM

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
0

#111 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 08 August 2014 - 10:35 AM

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/manua...goexception.php and deal with that but it might not be the best idea it might be better to catch the exceptions individually.
0

#112 User is offline   mrPascal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-August 14

Posted 08 August 2014 - 11:09 AM

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
0

#113 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 08 August 2014 - 11:38 AM

What would be the scenario for abstracting the Exceptions into the extension?

I mean what benefits would it create?
0

#114 User is offline   mrPascal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-August 14

Posted 11 August 2014 - 03:32 AM

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.yiiframew...ension/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
0

#115 User is offline   Sammaye 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 24-June 11

Posted 11 August 2014 - 05:57 AM

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?
0

Share this topic:


  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users