How to subclass Phalcon\Mvc\Router\Route? - phalcon

In Phalcon 1.3 I can return My\Mvc\Router that extends Phalcon\Mvc\Router.
Question: how can I swap in a custom matched route of My\Mvc\Router\Route type ?
Thanks!

If you declare a route in My\Mvc\Router it will overwrite Phalcon\Mvc\Router as one extends the other.

Related

overriding already overridden classes with a module - Prestashop 1.6

I KNOW THIS QUESTION IS NOT SPECIFICALLY TIED TO PROGRAMMING. BUT I REALLY WANT TO UNDERSTAND PRESTASHOP'S BEHAVIOUR OF OVERRIDING FILES VIA MODULES.
I want to extend a MYSQL table, let's say, Orders. A raw sql query in Module's install method would get this, no problem. But I wanna add this column to Order Model as well, which is where the main problem would come. Since I must override it like this:
public function __construct($id = null, $id_lang = null)
{
parent::__construct($id, $id_lang);
self::$definition['fields']['new_column'] = array('type' => self::TYPE_STRING);
}
My question is, how do I make sure that if override directory already has an Order.php with a __construct, how do I make it to merge changes rather than throwing error.. Is it possible?
Prestashop doesn't encourage using of overrides within modules at all. Because how I know there is no way to merge two overrides and only the last one will work if you will find a way to install a module with the second override. So they recommend extending existing classes and add all necessary data from there. For example, in your case, it should be something like this
class Order extends OrderCore
{
public $new_filed;
public function __construct($id = null, $id_lang = null)
{
Order::$definition['fields']['new_column'] = array('type' => self::TYPE_STRING);
parent::__construct($id, $id_lang);
}
}
and just include this class file inside your module. So if something similar will be included within another module no conflict should appear except if the properties will have the same name.

Laravel Query Builder : Where pivot not in

wherePivotIn is mentionend here (under Filtering Relationships Via Intermediate Table Columns) but I can't find anything about the opposite function.
As the wherePivotIn already exists but not the wherePivotNOTIn, I edited this file : vendor/laravel/framework/src/Illuminate/Database/Eloquant/Relations/BelongsToMany.php
And added this function
public function wherePivotNotIn($column, $values, $boolean = 'and', $not = false)
{
$this->pivotWhereIns[] = func_get_args();
return $this->whereNotIn($this->table.'.'.$column, $values, $boolean, $not);
}
Now the wherePivotNotIn exist and is working. But my question is:
Is it safe to update this file?
In case of update, I guess I will lose this...
After dinging a bit, I found out that the whereIn method accept more than 2 arguments.
We juste have to use it like that to use a "wherePivotNotIn"
->wherePivotIn($column,$value,'and','NotIn')
No need to declare a new class or using scope!
Yeah don't update vendor files that's a no no. Instead , create a class that extends BelongsToMany and put your implementation in there. You'll lose your changes as soon as the file updates.
Never edit what is inside the vendor directory. Create your own method to meet your need, in case it does not exist.
In your case, you can define a Local Query Scope
It will look like:
class Task extends Model
{
public function scopeWherePivotNotIn($query)
{
/**/
}
}
$tasks = Task::wherePivotNotIn()->get();

How set targetEntity from config file in symfony?

I am building bundle and i wat to use it in another application. I created Entity with user field and i want make config option with user class that must implement AdvancedUserInterface.
In Entity i use this mapping:
/**
* #ORM\ManyToOne(targetEntity="Symfony\Component\Security\Core\User\AdvancedUserInterface")
*/
private $author;
I know i can replace this Interface by adding to config.yml
//config.yml
doctrine:
[...]
orm:
[...]
resolve_target_entities:
Symfony\Component\Security\Core\User\AdvancedUserInterface: Draconicka\FosUserBundle\Entity\FosUser
But i think this is not good solution. I want put this class to bundle section in config. For examlpe
//config.yml
[...]
nattle_demo:
user_class: Draconicka\FosUserBundle\Entity\FosUser
Is this possible? Or better solution is override this bundle and add this field in each application?
I always use resolve taget entities - its easies way to fill targetEntity attr in your bundle.
Sometimes I use setting class in config if I need put class full name (with namespace) in other services.
For example, you want check user data in authListener. AuthListener has method supportClass ($class) { return $class == '\Your\Configured\Class' }

How can I resolve dependency in Castle Windsor Factory?

I read about factories in CastleWindsor but I cannot get it clear. Hope anyone could help me.
I have this typed factory in an MVC4 project.
public interface IOrderProcessorFactory
{
T Create<T>(string ProcessorName) where T : IOrderProcessor;
void Release(object service);
IOrderProcessor GetTakeAway();
IOrderProcessor GetInLocal();
}
this is register this way:
container.Register(Component.For<IOrderProcessorFactory>).AsFactory();
container.Register(Component.For<IOrderProcessor>).ImplementedBy<TakeAwayOrderProcessor>().LifestylePerWebRequest().Named("TakeAway"));
container.Register(Component.For<IOrderProcessor>().ImplementedBy<InLocalOrderProcessor>().LifestylePerWebRequest().Named("InLocal"));
If inside an MVC controller I call the factory in this way.
_orderProcessorFactory.GetTakeAway();
I get the correct one, the one named "TakeAway".
But for this I have to previous know the type. In other words, I want to call the factory get methods and pass a "name" and the factory returns the correct one.
For example in pseudo-code I want this
TakeAwayOrderProcessor processor1 = factory.GetMeProcessorCalled("TakeAway")
InLocalOrderProcessor processor2 = factory.GetMeProcessorCalled("InLocal")
I know I can pass parameters to the constructor but then I will have to select it "manually" with if name is this return this one else...
Is there any way Windsor can do this automatic, like StructureMap do with:
ObjectFactory.GetNamedInstance<IOrderProcessor>("InLocal");
You need a TypedFactoryComponentSelector

creating base model class in yii

In my application, i have fields that are common to all tables, like create date, update date etc. To assign these values i'm using beforeValidate callback. Now, this callback is same for all models.
To avoid code duplication, i want to create a base model class.
But, when I tried to create a base model, yii thrown error saying table cannot be found in database, which is true since I dont have any table for this base model.
Is there any way I can create a base model class.
Yes, if you work with dynamic DB structure or have other reasons to work with Yii ActiveRecord without creating classes for each table in DB, you may use smartActiveRecord yii extension
I separated it few minuts ago from my other extension -- AR behavior that adds versioning to any model (it copies all data on insert & update to special table (and create it if it's absent), that have a same structure as source table + "revision field" and primary key extended by this field.
Look at SmartAR.php source, there is example of usage in comments.
Take a look at CTimeStampBehavior.
Incase that doesn't help you, you can just write a behavior class yourself.
Hope this helps.
Edit:Assuming you are using ActiveRecords.
If you want to create a new base model, you can do this:
abstract class MyBaseARClass extends CActiveRecord{
protected function beforeValidate(){
if(parent::beforeValidate()){
// assign your fields
return true;
}
else return false;
}
}
Have you created a base table? Thinking about the Yii framework it may be easier to have a relationship between a model and the base model.
In your case, you need to override
public static function model($className=__CLASS__)
{
return parent::model($className);
}
in every child class so Yii would know which table to use for your model. Otherwise it will try and use base class as table name.
I.e.
class User extends BaseActiveRecord {
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}