Getting entity property name by database column with doctrine - php

I'm working on a import function that receives mapped data. The data is mapped by database column names for the target system. The application is symfony2 and uses doctrine to manage the database.
The problem is, most of the entity property names are different from the column names. I was wondering if there is a way to get the property by column name. Else i'll have to update the database without using the enities, or create another mapping.
Cheers,
Tim

Go through this class,
http://www.doctrine-project.org/api/orm/2.2/source-class-Doctrine.ORM.Mapping.ClassMetadataInfo.html
getFieldName() method, you can get field names.

Related

I cant edit and view posts in cakePhp tutorial

I watched cakePhp blog totorial and I have done all steps, but it shows error:
Error in: ROOT/src/Template/Posts/view.ctp, line 1
Could this be caused by using Auto-Tables?
Some of the Table objects in your application were created by instantiating Cake\ORM\Table instead of any other specific subclass.
This could be the cause for this exception. Auto-Tables are created for you under the following circumstances:
The class for the specified table does not exist.
The Table was created with a typo:
TableRegistry::get('Atricles');
The class file has a typo in the name or incorrect namespace: class Atricles extends Table.
The file containing the class has a typo or incorrect casing: Atricles.php
The Table was used using associations but the association has a typo:
$this->belongsTo('Atricles');
The table class resides in a Plugin but no plugin notation was used in the association definition.
Please try correcting the issue for the following table aliases:
Posts
I see my posts(which have been added in console), but I can't see one post, edit and add new.
You need to make sure that your Table object in your model is the same name as your table in your database. In this case your database table should be named articles. If you want to use a different database table in your model you can use this in your table object:
$this->table('my_table');
You can read more about this on their website: http://book.cakephp.org/3.0/en/orm/table-objects.html

Entity is not mapping to table name properly

I have created a Entity with name "InventoryVenue" for the table "Inventory_Venue".
When I'm trying to insert data, entity manager pointing to "InventoryVenue" instead of "Inventory_Venue" which is not exists in database.
This is my entity
This is my code
Here entity not pointing to the actual table.

What is the right Laravel class name for a given table name?

If I have database table suggestions_votes, what would be the correct name of Laravel (5.1) Class (SuggestionsVote or SuggestionVote)?
Table was created by migration, using
Schema::create('suggestions_votes', ...
Laravel recommends certain conventions, but they also provide you with options to override them.
If your model is "SuggestionVote", then the table associated with that model will be the snake case plural name of the class. In other words, it would look for the table "suggestion_votes". If you want to override the associated table name, you can add this property to your model:
protected $table = 'suggestions_votes';
If you are actually creating a pivot table for the models "Suggestion" and "Vote", then Laravel will by convention join the two related model names in alphabetical order. In other words, it will look for the pivot table "suggestion_vote". You can override this though when you define the relationship. For example:
return $this->belongsToMany('App\Suggestion', 'suggestions_votes');
Where 'App\Suggestion' would be fully namespaced path to your Suggestion class.
It depends. You can make any name work.
If you have no control of the database, the model 'should' be SuggestionsVote
If you do have control over the database, I would rename the table to suggestion_votes and the model name would be SuggestionVote

Interface or type-hint for single object and multiple object class in php

Recently started working with OOP in PHP. Following the "code to an Interface" principle, i got confused as to the type hint to use when passing a single object or multiple as argument to a method.
Currently, i have a "Student" class - represents a row in my students table, i also have a "Students" class that holds multiple student objects in an array.
To fetch the profile of one student, i pass the Students object (holding a single student object) to the profile class. I set a Students type hint in the profile class.
Now i feel this is bad code as i have lines like this
student = new Students();
and students = new Students();
question is,
am i on the right path?
if i remove the Students class and work with Student alone, based on the principle, how do i pass multiple Student objects (assuming array) to the profile class if it accepts a Student type hint?
what options do i have?
Thanks.
If by Students you mean a collection of Student objects, perhaps a better name would be StudentCollection or StudentSet.
There are two ways around the type hint problem:
Introduce a method on StudentCollection called ->getProfiles(); it would return an array of profiles for each Student instance it's managing by calling methods on Profile.
Introduce a (static) method on Profile that operates on a StudentCollection instance.
The first option has feature envy, which is why I've included a workaround.
Instead of reinventing the wheel you might want to try Doctrine or at least take a look at its architecture.
I'm not sure if I get your exact issue... But if you want to go for your own code I would first abstract the DB layer as well and have some base classes like Database, Table, Row, Field that an describe the DB stack and extend them as needed with some magic methods. So when you do Student extends Table it would automatically check for a "students" table or whatever else convention you like to implement. Alternatively you could just pass the table name as arg.
Whatever Object is returning the result set from the database would have to construct a single Row object for each row and add it to a collection of rows that I would name ResultSet and contains all the row objects and return that collection.

how to use non-database variables in doctrine model

I have two database fields $object->value and $object->max_value and want to store the proportion in $object->percent_value without having a db field for it.
Is there a way to do this in a doctrine model?
You should have an ObjectBase class (obviously named something different) where the database attributes are defined. There should also be an Object class. Here you can define the percent_value attribute which calculates and returns the percentage on the fly.

Categories