How does PHP ActiveRecord correlate the singular class name to the table name? - phpactiverecord

I'm still a bit confused by how the following works:
your table name would be "people"
class Person extends ActiveRecord\Model {}
So thats what i did with my table: posts.
class Post extends ActiveRecord\Model{}
At no point did I have to specify Post is actually equal to the table posts. So where/how does this magic happen? How would I override it if need be?
What about in instances like:
class fish = table fish
class goose = table geese
Thanks

Use the static $table_name in your model, see php-AR conventions.

Related

Zend Model access in singleton class - best approach

I'm looking for best pattern/approach to access one table data in singleton class (in ZF 1.x). In details:
I have one singleton class (just like Zend_Date for example) that make for me some basic abstract stuff very detached from application reality.
In this class, in two points, I need to access to one db table and I need to make some basic operation on it.
It's not a problem to use my regular ZF models class inside functions of this singleton. It works fine. Now it look like:
class My_ZF_Singleton
{
...
public function someFunctionInMySingleton()
{
...
$oModel = new Model_My_Model_Form_ZF_Application();
$oModel->letsDoSomeStuffWithDb();
...
}
...
}
But I feel in my bones that it's not a very good solution, not so glamour as I would like to be. It make my singleton class more attached to application then it should be. I would like to use some other pattern to access this db data then application model class. I would be very thankfull for any clue or better solution - it's not a "hey I'm stuck probem" or "hey I've got an error" - I'm just looking for better solution.
Not sure I quite understand your question or want the point might be, but I'll try.
In ZF1 the database adapter is typically a singleton already. Multiple databases maybe connected to but each will require a unique identification. Typical access to the default adapter setup in the application.ini or Bootstrap.php:
$adapter = Zend_Db_Table::getDefaultAdapter();
a common way to provide access to a single database table and give access to the Zend_Db_Table api is to build a DbTable model:
class Application_Model_DbTable_TableName extends Zend_DbTable_Abstract
{
protected $_name = 'Table_Name' //required if classname does not match table name
protected $_primary = 'primary_key_column_name'//optional, use if primary key is not 'id'
}
You can treat this class as an instance of the default database adapter for a single table (works really well in a mapper). You can also add functions to this class to override or add to the default Zend_Db_Table api.
I hope this at least comes close.

EF Codefirst, One class, multiple tables with discriminator

I doing a little investigation and I am wondering if the following is possible.
I am looking to create a BaseEntityWithDetails class that I can reuse for any type that I would like to have extendable. For example
public abstract class EntityDetail
{
}
This class is used to persist a key and value for the entity.
"Products" would be extended by doing the following...
public class ProductDetail : EntityDetail
{
}
public class Product : BaseEntityWithDetails<ProductDetail>
{
}
The base class "BaseEntityWithDetails" will provide some helper methods for setting and getting. What do you think?
What is the most effective way of mapping this with EF CodeFirst while being super easy to allow another type implement an DetailsEntityTypeConfiguration like the following
public class ProductMap : DetailsEntityTypeConfiguration<Product, ProductDetail>
{
}
Thanks in advance!
I would like to quote someone really smart on this: Reuse is a fallacy. Don't bother doing stuff like this because it will only make your design more obfuscated and complex. Save your inheritance to the entities in your domain which really share the same behavior, don't do this type of assumptions up front.
As a side note: You can map this as a table per type if you put your "EntityDetail" into your database, but as I said before, this is just not a good idea.

Accessing other classes defined in a Model Class file in Zend Framework

I have a model class "Application_Model_Person" located in application/models/Person.php that also defines other classes, like Gender and Age:
class Age
{
...
};
class Gender
{
...
};
class Application_Model_Person
{
...
}
My problem is that I want to access Age and Gender in a controller, but I don't know how. Calling new Application_Model_Age doesn't work because Age.php doesn't exist. I want these classes to stay in Application_Model_Person because they are strongly related.
Any ideas?
Well, either create Application_Model_Age and Application_Model_Gender include the File via
require(APPLICATION_PATH . '/models/Person.php');
$age = new Age();
$gender = new Gender();
I would suggest the first way though. Several Classes in one named File ... might be a personal opinion, but i think that's not the most straightforward thing to do. Even though i understand the relation between the three Classes ;)

Extend model class in ASP.MVC (inheritance?)

i'd like to create something like wrapper or mayby better word would be "Extension" for generated in EntityFramework model class...
I've got model USER, with password, username etc... and user is in relation many-to-many with some other objects... whatever...
I'd like to create something like this:
class ExtendedUser : USER {
public void AddObject(Object o) {}
}
But i don't know, is it good idea...
I don't know how to create constructor. I'd like do something like this.
User u = ...;
ExtendedUser eu = u as ExtendedUser;
Conceptual i'd like to fetch data from DB and put it into ExtendedUser instance, because this object will have methods to manipulate on this data...
How to do this?
I believe that the classes generated by the entity framework are partial classes, so you could create another partial class with the same name, within the same namespace, and you should see any extra methods that you add on the user class, e.g.:
partial class User
{
//Generated code
}
partial class User
{
public void MyMethod();
}
User u = new User();
u.MyMethod();
If you just want to extend methods, that's enough. However if you also want to add metadata to your model (like data annotations, etc.) this approach doesn't works.
In fact, you can only add methods to the auto generated class.
I answered a question about adding and preserving data annotations to auto generated entity classes, here.

Can a Grails Domain Class inherit from a class that is not a domain class?

I tried to do this, but it insists on their being a table of the base class. I tried using tablePerHierarchy false as well and that didn't make any difference.
I have a few domain classes that share a bunch of methods that operate on an inherited transient field. I had hoped that just having the class over in the non-domain section "/src/..." would be enough for GORM to realize there isn't a base class in the database.
When I put the class in the src section I get a different error. It no longer maps the specific class.
Error 500: org.springframework.orm.hibernate3.HibernateQueryException: Summarypage is not mapped [from Summarypage where id = ?]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Summarypage is not mapped [from Summarypage where id = ?]
I figured it out. If the base class is an abstract class it does the right thing.
Thanks anyway everyone.