SELECT
*
FROM table1
LEFT JOIN table2
ON table1.id=table2.ref
How to distinctly identify column which are from table1 or table2. Is there anyway to get table name in alias which can be used to identify column by table name?If it matters i am using PHP to fetch from database.
Well, your reason is fair but the implementation is unusual.
When each table represents a class in php, it is called ORM - Object-relational mapping.
And when it is used, an object already knows it's fields, because each table being only a reflection of the class properties. Frankly, with ORM nobody is going to create tables manually - they are created (or altered) based on the object properties.
So, an object always can tell it's fields from others.
As for your literal question - no, there is no way to get such info, as far as I know
Related
Right now I'm trying to query a table that holds the id of two tables that holds the id to two other tables (where one of these holds the id to other tables)
Hell, I know.
As I am a beginner in SQL, how do I go about efficiently querying this to get all the details of each table? As of now i only see a very long set of Select statements. Here is how the tables are set up below
I am not sure what you are asking.
If you need a SELECT command it would have 2 INNER joins. But, if you are asking to get all the info (and children) then it would need more INNER joins. OR if you are using a persistent client for your application.... I just don't know. Because it is incomplete data to do it.
I'm working on a web project that contains a mixture of Doctrine and traditional inline SQL for database access. We're migrating from the latter to the former over time.
One of the database tables (Table A) has an informal foreign key (i.e. there are no actual constraints in the SQL table definition) column. The value in this column is nullable, but sometimes the value in this column refers to a another table's (Table B) primary key that has since been deleted.
Table A's relationship with Table B is formalised in Table A's entity definition in Doctrine. When I create an instance of an entity from an existing row in Table A and the informal foreign key column has a value that is no longer in Table B, then it seems an invalid Table B entity is created. I.e. the object is set, and I can query it's id through the TableB->getId() method we have authored, but any other TableB->getProperty() fails and throws an error.
Since I'm already working with historic data, I need a run-time solution to this. I need to identify via a check if the Table B entity is valid.
I could put it in a try-catch loop, but this is not a common pattern in our code base and does not seem very elegant.
Is there a canonical method for checking the validatity of a Doctrine entity to solve this problem?
Thank you
As I see it, there are 2 things you should do.
First fix your data corruption, this is a must. If A points to a non-existing B, then A should not be pointing at all. SELECT a.id FROM table_a a LEFT JOIN table_b b ON b.id = a.table_b_id WHERE b.id IS NULL. This query will select all FK that are not existing anymore, you can simply update the a.table_b_id to NULL with that list.
Second, you should (not in your getter) take care of the relation issue in your code. I'm not sure if Doctrine throws an EntityNotFoundException or only does this with find($pk), but I'm sure you can check if the return of your getter is null or catch that exception.
Or is it enough to have just the relations defined in the model. I have been trying to connect tables users and groups and got only to a point where only last connection in array got saved ...
Just to add, I am using table users_groups as a join table. This table tas fields user_id and group_id. Find all works like a treat.
All you need to do as far as your database is concerned is to make sure you're using the correct table name and field(s). Then, as long as you have your model associations set up correctly, you should be good to go.
In your case, your table should be 'groups_users', not 'users_groups' (they should be in alphabetical order).
I can't find how to insert a row that doesn't have a PK. This isn't possible with a class that is extending Zend_Db_Table_Abstract, so how should I do this?
The table I in which I want to insert records is a join table.
The only two columns in it could serve as a PK, but I don't know how I should let Zend know that neither.
Any suggestions?
Zend doesn't really care if the table have in fact a PK (or an index) or not. The primary_key option in a Zend_Db_Table_Abstract is really just to know which column the 'WHERE' clause will search for when using find and find* methods, etc. So, basically, just specify which columns to use as reference columns for your db model. I can't give you more details since I don't know the details of the said table.
I have a MySQL table with clients in it, the usual data, names, addresses, phone numbers etc etc i also have a field which is called 'roles' in which a client ticks off what they like to do i.e coding, graphic design, illustrations etc etc .. this data gets pushed into the field serialized with each roles code, the following is an example.
a:3:{s:4:"_wfa";s:2:"on";s:3:"_CS";s:2:"on";s:3:"_CM";s:2:"on";}
On a 'viewall' page, i need to output all the details for a user that has ticked a specific box, as an example, i need to output all users that have ticked the '_wfa' box.
I hope this makes sense, i cant seem to figure out how to do it.
I hope someone can shed some light on this.
Cheers,
You should never have more than one value in a single column of a row. Store the roles in their own database table, with the user's ID, and you will be able to simply ask MySQL for the users with a role as desired.
CREATE TABLE user_roles (user_id INT, role_name VARCHAR(100));
INSERT INTO user_roles (1, '_wfa');
INSERT INTO user_roles (1, '_CS');
INSERT INTO user_roles (1, '_CM');
SELECT users.id FROM users INNER JOIN user_roles ON users.id = user_roles.user_id WHERE user_roles.role_name = '_wfa';
You should normalise that into a table. Having it serialised means you can not use any of the benefits of SQL on it, and also that parsing it requires PHP (or custom code for other language).
MySQL, or any database, can not unserialize data performed by an external programming language. The only way to get the data out, is to pull it out and unserialize in PHP before you can use the data.
The only way to get any value out of using a database is to store data in it, using tables and native data types to enforce data consistency. Normalization and referential integrity work to minimize data duplication while enforcing business rules.
Transitioning to SQL, objects become tables -- they're like arrays. Object attributes become columns, but when an object contains an array of other objects - that attribute gets promoted to being a table... Normalization means taking things like roles, and making a code table for them that you can refer to in other tables.