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.
Related
I have multiple tables in a Laravel app with 1-to-1 relationship such as users , users_settings , user_financial
And some 1-to-many relationships such as users_histories
My questions are:
1. Should I always include incremental id at the first?
for example is the id necessary in the Table #2 below?
Table 1:
id (primary,increments) , name, email, password
Table 2:
id (primary,increments), user_id, something_extra
^ why does every guide include this? // e.g. https://appdividend.com/2017/10/12/laravel-one-to-one-eloquent-relationships/
Can't I just use user_id as primary key and skip the incremental key? because I want to auto insert it on table 2 as soon as data is inserted in table 1.
2. How should I name 1-to-1 and 1-to-many tables in Laravel? `
I searched but didn't find any naming convention for different type of relationships...
Currently I do:
users table with primary key id is the base.
1-to-1: users_settings with foreign key user_id
1-to-many: users_histories foreign_key user_id
many-to-many: users_groups foreign_key user_id
should the first two tables be named settings/setting , histories/history instead? sorry I'm a little confused here.
I actually asked a similar question around 2 days ago. Its up to you but I'd say yes. In my case if I don't auto_increment all my ids in the related tables, data won't be associated with the correct user. However, there is an argument for saying auto_increment columns should not be used in this case, but they are useful for other things. According to some, the relationships might not be as meaningful so it'd be up to you and down to the specifics of you data tables for how meaningful the relationship will be. Regardless, you should research more into the advantages of auto_incrementing all your ids in related tables, as well as possible disadvantages before deciding what you want to do. Either way is fine, but they offer different advantages and disadvantages- which you'll need to compare and what works best for your specific case.
This is a well debated topic about the primary key. IMHO, No, you shouldn't. Every column in database should have a purpose. Following this, for your example, I agree that the auto_increment id is redundant and this is simply because it doesn't have a purpose. The second table is still uniquely describing the user so that the primary key should be the user_id.
Beside the above, there is another principle for me to decide whether I need the auto_increment id: whether I can see a table as an entity. For example, user is clearly an entity, but a relationship is not (in most cases), i.e., composite key can serves the purpose. But when an relationship table is extended to have more attributes and it starts to make sense for it to have an auto_increment id.
I don't have much experience on Laravel, but the naming for a database table should not be dictated by a framework. Comparing history and user_history, what a new DBA or developer expect from the two names without looking its data? user_history describes the table more precisely
The simplified info: I have a database where I have a table with columns 'code1' and 'code2', these are used to make an id for another table where the id column is 'code1.code2'. The columns are varchar columns.
Full info: If you are wondering I am working on an importer for geonames database and the geonames table has country_code and admin1_codes and they have another table called admin1_codes and that table has an ID column of country_code.admin1_code (concataneted with dot). If you need further info about geonames database, it is freely available at their web site.
So I want to make a relation using eloquent models but it seems impossible. I am thinking that I need to make my own custom db relation class for this.
So the question is, what would be the best way to accomplish this task?
Thanks!
UPDATE
I came up with a workaround using a scope:
$query = $query->leftJoin('table2',function($join) {
$join->on(
DB::raw('table2.code = CONCAT_WS(\'.\','.
'table1.code1,'.
'table1.code2)'),
DB::raw(''),
DB::raw('')
);
});
return $query;
This successfully joins the relevant row while matching the codes. However I would like to setup a relation between tables. Any ideas on if this is even possible using Eloquent? or can you think about a more elegant solution?
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).
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
How do I retrieve a multi-column PK in MySQL?
For example I have my primary key setup as
PRIMARY KEY (donor_id,country_id)
Now if I want to get the primary key value without concatenating those 2 fields in a select query, how do I do that? I want to use this in a view (or better yet, directly in phpmaker).
It's not clear what you mean by "without concatenating". A simple
SELECT donor_id, country_id FROM table WHERE ...;
will retrieve the records; you don't need to apply a CONCATENATE() function or anything like that. This is the Right Way to select two records from a table; the fact that they both happen to be declared part of the primary key changes nothing.
No special way is needed to get the records from table that has a multi-column PK in MySQL. Things might be different if you are using an ORM. An ORM may or may have special or different syntax/features for working with tables with multi-column PK.