How can override ActiveRecord find() method with two behaviors?
I am using two behaviors in model and each of one has override the find() method.
Thanks
Related
I'm using SQLAlchemy and am generating classes dynamically for my database via the Automapping functionality.
I need to add a Mixin class with various helper methods to each of these automapped classes.
I tried to create subclasses of the automapped class with the mixin class:
db = create_engine(connection_string)
automapper = automap_base()
automapper.prepare(db, reflect=True)
for class_variable in automapper.__subclasses__():
new_class = type(class_variable.__name__, (class_variable, Mixins), {})
when I try to use these classes I get errors like:
class _ is a subclass of AutomapBase. Mappings are not produced until the .prepare() method is called on the class hierarchy.
If I call automapper.prepare() again, I get warnings like this and mostly just enters an infinite loop:
SAWarning: This declarative base already contains a class with the same class name and module name as sqlalchemy.ext.automap.payments, and will be replaced in the string-lookup table.
I cannot specify the classes explicitly as in this answer, because I don't know the database tables ahead of time.
From the docs, you can augment the Base with your Mixin class. In this case, you could pass your Mixin as the cls parameter.
automapper = automap_base(cls=Mixin)
I have some models with their ActiveQuery defined with some default conditions.
class ProfileQuery extends \yii\db\ActiveQuery{
public function init(){
$this->andOnCondition(['not',[Profile::tableName().'.status'=>2]]);
parent::init();
}
...
The problem is if in another model search I use a join with an alias the init() function of ProfileQuery still tries to search tablename.status
Is there a way to get the current alias while inside the init() function?
This is not possible right now. Limited alias support was problem since the beginning of Yii 2, but recently it was postponed to Yii 3 and most likely will not be fixed in Yii 2: https://github.com/yiisoft/active-record/issues/33
There were some attempts to fix this, but none of them has been finalized and merged. If you really need it, you may use some code from these PRs and implement it yourself in custom component or fork:
https://github.com/yiisoft/yii2/pull/10253
https://github.com/yiisoft/yii2/pull/10813
https://github.com/yiisoft/yii2/pull/11646
Since Yii 2.0.16 you may also use ActiveQuery::getTableNameAndAlias() but it probably will be useless in init().
If we have a class:
class Customer(val customerName: String) { }
Its contructor parameter customerName is accessible through getCustomerName() (because it's also a property). If we want to restrict access to this property we should declare it as private.
Since in many cases from Java world (and if a class is not intended to be data class) fields which are assigned from constructor parameters are for private / protected use it feels like an additional effort to explicitely declare them private in Kotlin.
Also, Kotlin classes are final by default, so why not follow this principle for properties? Am I missing something?
In our experience, and from some empirical studies of existing codebases, internal/public visibility is best for properties.
Also, Kotlin classes are final by default, so why not follow this principle for properties? Am I missing something?
Properties are final by default, i.e. they can not be overridden unless you supply the open modifier explicitly.
A Protected Modifier in Kotlin: CANNOT be set on top-level declarations. Declarations that are protected in a class, can be accessed only in their subclasses.
for more details you can use this link
In AS3, the below method accepts a parameter of any type:
public function myFunc(data:*) :void
Is it possible to limit the type to a specific package? Something like this maybe:
public function myFunc(data:(my.package:*)) //Accepts any type from my.package
It is possible, but will only have type control at runtime.
import flash.utils.getQualifiedClassName;
public function myFunc(data:*):void {
if (data is Object) {
var fqcn:String=getQualifiedClassName(data);
if (fqcn.slice(0,10)!='my.package') return; // otherwise work
// work here
} // simple types process if needed
}
This sounds like a design issue. One way to make this work during compile is if the parameter type is a custom class:
public function myFunc(data:MyCustomClass):void
Assuming that all the classes within my.package are varied, you could create a custom base class that extends Object and have all of your classes within my.package extend from this base class. Of course, If, however, the inheritance of your my.package classes is less broad you wouldn't need to reach so far. For example, you should only extend from DisplayObject if all the classes within my.package are of that type.
There may also be a way to accomplish what you want using namespaces, but I'm unsure.
All Objects in actionscript3.0 inherit from the Object class, but the actionscript3.0 compiler seems not to be smart enough to understand this.
take a look at the following code:
package{
public class TestOne{
public function TestOne(){
var t2: TestTwo = new TestTwo();
trace(t2.toString()); // COMPILE TIME ERROR
trace((t2 as Object).toString(); // [object TestTwo]
var t22 : * = new TestTwo();
trace(t22.toString()); // [object TestTwo]
trace((t22 as Object).toString(); // [object TestTwo]
}
}
}
class TestTwo{}
t2.toString() gives a compile time error because the data type t2 does not include toString(). However, t2 does include toString() because it is an object as (t2 as Object).toString() shows. If we do not give the variable a datatype, like t22, then the problem is never encountered. Why cant the actionscript3.0 compiler relize that t2 is both TestTwo and Object?
This is because
Methods of the Object class are
dynamically created on Object's
prototype. To redefine this method in
a subclass of Object, do not use the
override keyword. For example, a
subclass of Object implements function
toString():String instead of using an
override of the base class.
So if you cast TestTwo to an Object, the compiler knows those methods will be implemented. If you don't cast it, TestTwo does not inherit those methods and so they have not been implemented and will error.
It's a bit of a weird one!
Class inheritance and prototype inheritance are two different things in AS3. Prototype inheritance is as far as I understand included for backwards compatibility with AS2/AS1, and it's prototype inheritance that's providing the toString() method on the variable that's cast to the Object class in your example. The Object prototype has the toString() method, not the Object class. Since the prototype is not present on your TestTwo class, it doesn't have a toString() method. If the toString() method was supplied via class inheritance instead, your example would compile.
So technically, your statement "All Objects in actionscript3.0 inherit from the Object class" is not entirely correct, because of the difference between prototype inheritance and class inheritance. Prototype inheritance is a somewhat esoteric OO concept found in script languages.
Does anyone know the reason why AS3 is designed like this?