New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The getObjectModel() method should work for all ActiveRecord classes #6839
Comments
It's not really a common approach for extending multiple classes no? Normally you'd use PHP magic methods |
Use case: class Product extends ActiveRecord {...}
class ProductForm extends Product {...} I use ProductForm to attach images. |
In the FAQ we simply use the following; Ticketclass Ticket extends ContentActiveRecord implements Searchable {...} TicketSearchclass TicketSearch extends Ticket {...} Then set I also do the same method for the Faq model; class Faq extends ActiveRecord {...} class FaqSearch extends Faq {...} |
@marc-farre Are you sure your example here isn't reversed? I haven't tested it, but I would expect files from In general, I'm not a big fan of the Since it seems only relevant regarding PolymorphicRelations, I am thinking about an interface like And What do you think about this? @yurabakhtin What do you think? |
@luke- yes, sorry, I've updated my example (below, a more understandable use case). I agree, this But in another hand, I think we should have the possibility to extend any So I think that restricting PolymorphicRelationTargetInterfaceYes, the Another approach, more complicated in the code, but that could avoid It could be done this way (not tested): function getCloserParentClassWithTableName($object) {
$class = get_class($object);
if (method_exists($class, 'tableName')) {
return $class;
}
while ($class = get_parent_class($class)) {
if (method_exists($class, 'tableName')) {
return $class;
}
}
return NULL;
}
$b = new B();
echo getCloserParentClassWithTableName($d); // 'A' A more understandable use case:class Product extends ActiveRecord
{}
class ProductForm extends Product
{
public static function getObjectModel(): string
{
return Product::class;
}
} A form allows attaching files to The problem is that in the table |
@marc-farre I agree with that. Since the File module is based on polymorphic relations, we must have this option at the ActiveRecord level.
Interesting idea. To summarize: Always use the class in hierarchy which directly implements the Hmm, actually, we can also keep the name |
@luke- yes, you're right (as usual 😉!). But it's possible to detect in which class the class Product extends ActiveRecord
{
public static function tableName()
{
return 'product';
}
}
class ProductForm extends Product
{
public static function getObjectModel(): string
{
return Product::class;
}
} In function tableNameIsDeclared($className)
{
$reflector = new \ReflectionClass($className);
$method = $reflector->getMethod('tableName');
return $method->class === $className;
}
function getObjectModel($object)
{
$class = get_class($object);
if (tableNameIsDeclared($class)) {
return $class;
}
while ($class = get_parent_class($class)) {
if (tableNameIsDeclared($class)) {
return $class;
}
}
return null;
} $object = new ProductForm();
echo PolymorphicRelation::getObjectModel($object); // returns humhub\modules\myModule\models\Product So should we implement this and remove |
Hmm, I would prefer to move "getObjectModel()" to AR. The other solution is also interesting. The only question is whether the class with "tableName()" is really always the right one. And if not, how could it be overwritten? |
Wouldn't using Example<?php
namespace app\models;
use yii\db\ActiveRecord;
class BaseModel extends ActiveRecord
{
public static function tableName()
{
return 'default_table_name';
}
} <?php
namespace app\models;
class CustomModel extends BaseModel
{
public static function tableName()
{
return 'custom_default_table_name';
}
} Another method would be <?php
namespace app\models;
use yii\db\ActiveRecord;
class BaseModel extends ActiveRecord
{
public static function tableName()
{
return 'default_table_name';
}
} <?php
namespace app\models;
class CustomModel extends BaseModel
{
// Your custom implementation
public static function getTablePrefix()
{
// You can dynamically determine the table prefix here
return 'dynamic_prefix_';
}
public static function tableName()
{
return static::getTablePrefix() . 'default_table_name';
}
} |
What steps will reproduce the problem?
Attach a file to B
What is the expected result?
1 (the file is attached to A).
What do you get instead?
0 (the file is attached to B).
Additional info
This is because
PolymorphicRelation::getObjectModel()
only works forContentActiveRecord
orContentAddonActiveRecord
:https://github.com/humhub/humhub/blob/master/protected/humhub/components/behaviors/PolymorphicRelation.php#L119
@luke- would you accept a PR where:
is moved from
ContentActiveRecord
andContentAddonActiveRecord
toActiveRecord
?And in
PolymorphicRelation::getObjectModel()
I'll replace:with:
The text was updated successfully, but these errors were encountered: