So I override AbstractAdmin because I wanted to add some functions and that all of my admin classes would have it.
It looks like this:
<?php
namespace AdminBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
class BaseAdmin extends AbstractAdmin
{
....
}
As you can see my BaseAdmin extends the default sonata AbstractAdmin class. Everything works fine and all my classes have the custom functions if I extend the new BaseAdmin like: class ProductAdmin extends BaseAdmin
But I noticed one strange thing...
I have a class that uses 'file' type to upload images.
If I extend my admin class with BaseAdmin the form field 'file' is made into a textarea (all other simple textfields are transformed to textarea too for some reason). If I extend the default AbstractAdmin everything works fine..
Do I need to specify my new admin class somewhere in the configs maybe or what is the problem here?
You should show us some more code but from what you already told i would say this is because you override methods in the ProductAdmin class.
In your BaseAdmin class you override AbstractAdmin methods and you call parent::method() but when overriding a method in a class extending BaseAdmin you have to call AbstractAdmin::method() instead of parent::method() as you need to call the grand parent method and not the parent one.
Hope that helps
Related
I want to override
vendor\laravel\framework\src\Illuminate\Auth\Password\DatabaseTokenRepository.php
I tried this user model in app folder.. but that is not working.. Can you tell me where to put it?
So see this is a vendor class. If you want to override any functionality of that class you can do so by applying method overriding. Just extends the class that you wan't to override, then redefine the function that you want to override in your class. Now, you can use your own class whenever need instead of the vendor class.
For example:
class TokenRepo extends DatabaseTokenRepository{
//Define the functionality here to ovrride
}
Usages:
$token = new TokenRepo();//instead of original DatabaseTokenRepository
I have two models: models/Categories.php and models/backend/BE_categories.php
I want BE_categories class to extend Categories, since it has some useful functions I can re-use:
Categories.php
class Categories extends CI_Model
{
...
BE_categories.php
class Be_categories extends Categories
{
...
But when $this->load->model('models/be_categories') I get the error: Unable to locate the specified class: Session.php yet it works with exending CI_Model What have I done wrong?
Just my guess, you load Session library inside some function in Categories model,and then try to use function with session library specific function inside model BE_Categories.
Add Session library in config/autoload.php or load it inside Categories model constructor, and remember to add constructor inside BE_Categories.
I also had a Controller called Categories.php, with class Categories, which is where CI was getting confused.
I had to rename models/Categories.php to models/Categories_model.php as well as the class name.
But also had to include the file when extending:
BE_categories_model.php
include('application/models/Categories_model.php');
class Be_categories_model extends Categories_model
{
...
Edit:
Thanks to commenter, might be better to to include this model in config/autoload.php instead of using PHP's include() function.
I'm using Jacopo Authentication package in one of my sites, I'm extending it to add some methods and do stuff after I call its methods, I'm extending it like this:
<?php
use Jacopo\Authentication\Controllers\UserController as JacopoUserController;
class UserController extends JacopoUserController{
...
}
Now, the problem is that I need to use some methods from my BaseController, whats the best way to be able to use BaseController here? Should I just instantiate it? Maybe move as much logic as I can to a Model or Helper and duplicate a little code? Is there anything like this?
class UserController extends JacopoUserController extends BaseController{
Thank you in advance.
You can extend only one class in PHP. However you could create trait and put there functionality from Basecontroller and to use this functionality simple add use Traitname; to class that needs that functionality
We are looking to build a system with core classes and the ability to extend these core classes and are looking in to using namespaces.
The problem we are having is working out if we can extend an extended class without extending the class that it extends from
For example, if we have folders and files as below
shared/classes/Entity.php
shared/classes/DatabaseEntity.php - Extends Entity.php
shared/classes/User.php - Extends DatabaseEntity.php
classes/ - Holds classes which extend from the shared classes
If we wanted to create a custom DatabaseEntity class without creating a custom User class , is this possible?
The way I understand this is that the User class will be looking in the shared namespace to extend the DatabaseEntity class but as we have extended the DatabaseEntity class, it needs to look at the top level classes directory
Example of shared/classes/User.php
namespace shared;
class User extends DatabaseEntity {
}
Example of shared/classes/DatabaseEntity.php
namespace shared;
abstract class DatabaseEntity extends Entity {
}
Example of classes/DatabaseEntity.php
namespace custom;
use shared\classes\Entity;
abstract class DatabaseEntity extends Entity {
//Some custom functionality to extend shared/DatabaseEntity
}
So if we didn't want to change the User class to say
use custom/DatabaseEntity
Then is this possible?
Hopefully that makes sense
Thanks in advance for any help
If you don't want to add to User class
use custom/DatabaseEntity
and you want to extend custom/DatabaseEntity
you may just change class declaration from
namespace shared;
class User extends DatabaseEntity {
}
to
namespace shared;
class User extends \custom\DatabaseEntity {
}
if you want to extend \custom\DatabaseEntity.
If it's not want you want to achieve I cannot understand your problem - you ask two questions.
You asked
If we wanted to create a custom DatabaseEntity class without creating
a custom User class , is this possible?
The answer is - yes, you just created it in your example. You created custom DatabaseEntity class without creating custom User class.
But if you want to achieve:
it needs to look at the top level classes directory
you need to tell User class to extend specific class - so you will need to extend using fully qualified class or import namespace using use and creating alias
I don't know if I understand you well, but you want to create CustomDatabaseEntity class that will extend DatabaseEntity and you don't want that CustomDatabaseEntity extends User class.
It's of course possible. You can create as many child classes as you want. As User class is defined that it extend DatabaseEntity class it will even don't know that you created CustomDatabaseEntity
I also think that you are using it a bit wrong. If DatabaseEntity have anything common with database and not with User itself, you should rather create Interface DatabaseEntityInterface, those two DatabaseEntity classes should implement interface
and then in User class you should pass it as constructor argument
class User {
protected $dbi;
public function _construct(DatabaseEntityInterface $dbi) {
$this->dbi = $dbi
}
}
and later you can pass to User class either class for shared folder or the one from classes
I am building a series of forms, and I am trying to inherit the functionality of a parent Form class into all the forms. For example,
LeaveForm extends Form (Model)
LeaveFormController extends FormController
I am handling all the leave form specific stuff in LeaveFormController and LeaveForm.
In LeaveFormController constructor, I simply call the parent class constructor, then load the LeaveForm Model. And in FormController constructor, I load Form model.
My problem is, I get an error,
Cannot redeclare class form in Form.php
Have I got my architecture wrong? How do I handle this ?
check if the class has already been initialized like this:
if (!class_exists('classname'))
{
// ok fine create new instance now
}
Possibly when you $this->load->model('Form'), you manually included the models/form.php file?
In your leaveform.php model file, make sure you load the superclass model you extend using codeigniter's model loading mechanism instead of require or include. Codeigniter has a loader that keeps track of already-loaded files to avoid redeclaring classes, but you need to use $this->load to use it. It won't know about files loaded directly with include or require.
So at the top of leaveform.php, use this:
$CI =& get_instance(); $CI->load->model('Form');
This is not related, but you will have pain unless you namespace your CodeIgniter model classes the same way you namespace Controller classes.
Try using FormModel extends CI_Model {}; Instead of Form extends CI_Model {};