Using doctrines findBy/getBy magic methods with the pager object? - php

Is it possible to use doctrines magic methods (in this case findBy*) with sfDoctrinePager? I'm getting the following error:
Call to undefined method Doctrine_Collection::offset()
Here is the code:
$this->pager->setQuery(Doctrine::getTable('notification')->findByUserId($this->getUser()->getGuardUser()->getId()));
I know I can build a query myself just want to know if it's possible using the "magic" methods.

nope, you can't. the findby* methods returns an object or an array of objects. sfDoctrinePager can only work on query objects.

Related

Methods inside Methods PHP

The following code is the sample code:
$sample= $module->get('data')
->anotherModule
->find(true);
I couldn't understand what exactly is the object hierarchy in here. According to my knowledge, there must be an object $module and that class has a method get which takes the parameter as 'data'.
But according to the code, still it gets deeper to anotherModule and find method. Can anyone explain me what is happening in this code?
This is called method chaining.
A method returns an object which contains other methods. You can find an example for that here: PHP method chaining?

How to mock an Eloquent Model using Mockery?

I'm trying this:
$this->dsMock = Mockery::mock('Eloquent', 'API\V1\DataSet');
$this->app->instance('API\V1\DataSet', $this->dsMock);
$this->dsMock->shouldReceive('isLocalData')->once()->andReturn(true);
Then, inside the class under test:
$test = DataSet::isLocalData($dataSetId);
However, the DataSet class is not being mocked. It's still trying to access the database. Why?
The likely problem is Laravel's unfortunate over use of Façade's (which are also factories). If DataSet was already instantiated using the Façade, it will keep returning the same class, you won't get a mocked version.
I can't remember off hand if you can instantiate your class without using the Façade in Laravel. You have to remember that when you call DataSet statically in the application, you're actually not referencing API\V1\DataSet but something else that manages it.

How can php's foreach loop work on Laravel's Eloquent/Collection objects?

It just occured to me that I can loop through a $collections object returned from Model::where(), even though its not an array. Doing some googling shows looping through an object goes through all of the object's parameters, which isn't what happens to the Collection object. How exactly do they do this in Laravel? What is this black magic?
I guess maybe more generally, how can you set an object up that it is compatible inside foreach()?
If your object implements a specific interface or extends a specific class, PHP knows how to iterate it:
http://www.php.net//manual/en/language.oop5.iterations.php
There is of course no magic involved here, you can do it yourself in your classes. The only thing you have to do, is to implement the IteratorAggregate interface, from then on your class can be used in a foreach loop.

PHP => Call to non-existing class constants, way to handle?

just like the __call magic method we can use in PHP to hook a call to a non defined method, is there a way to hook a call to an undefined constant or variable?
Like A::B, where B doesn't exist.
No. Constants are evaluated at opcode compile time not runtime, so there's no way to 'catch' them. There is still an issue with php related to this where a parent class cannot call a childs constant inside a parents method.
You're probably looking for __get and __set. I'm not really sure how it works with static constants though, you might only be able to work with instanced objects. See G-Nugget's comment regarding using them statically.

php chain calling static non objects

I can make infinite calling by returning $this in class body functions after creating as object.
Is it possible to make the same without creating object?
Something like myclass::func1()::func2()::func3()
i was trying with reflection and stored new class in parent variable, but it returning also object, which couldnt be called by ::
No, it is not possible to chain static methods.

Categories