Laravel/Eloquent relation using id value combination of 2 columns - php

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?

Related

how to get data from a table which data is not available in joined table in cakePHP

I am using cakePhp 3.6, I have two table. Both are joined. In one table I have some token table name is codes and on another table I have stored some code for some venue. Table name is venues_codes.
Like I have 10 codein codes table. And in venues_codes table 5 code is stored for some vanue.
This is the structure for venues_codes table.
id venue_id code_id.
Here venue_id is coming from another table. It is not my concern now.
I just want to get the rest five code from codes table that is not available in venues_codes table.
This is how I have tried, but it is only fetching token that is available in both table.
$this->loadModel('VenueCodes');
$this->loadModel('Codes');
$query = $this->VenueCodes->find()
->contain(['Codes'])
->select(['id','code_id']);`
I am working with CakePHP. But if anyone can help me with the normal sql logic, that also will be helpful for me.
This should give you all the codes that are not connected to a venue:
$codes = $this->Codes->find()->notMatching('Venues');
Pure mysql query would look like this:
SELECT code FROM codes WHERE code NOT IN (SELECT code_id FROM venues_codes);

What is the point of making relation in phpmyadmin database designer?

I understand some basics about relational database. But I don't get the point of making relation through phpmyadmin designer. What is the benefit there when I have to query any related table with another table's content ID?
When I make any query to select post where user_id=1, is there any way to make it like that, I will select from user_list where id=1, and I don't have to make another query to table posts?
To answer the first question: It documents the relationships for reference, and most designer applications will generate constraints enforcing those relationships.
To your answer your second question, no. If you only want information from posts, there would be no reason to involve users_list unless it relied on information from there, such as wanting to know "posts made by any users with the first name 'bob'"; in which case you would use a join. But if you already know the id for the user, there is no reason to involve users_list.

When using cakephp habtm, do I have to create relations in MySql too?

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).

Create table for each id existent in another table

I was wondering if there is a way to create a MySQL table for each ID that exists in another table. I think that would be fairly easy doing it with PHP, but I'm not sure if that can be done with MySQL.
So for instance I have a table called users which has an X amount of columns. One column is the IDs column. So I would like to iterate through that column and grab the IDs and create for each of those IDs a new table which will have the name of "user_specific_id_ " + ID. So for the ID 1 the name of the newly created table would be user_specific_id_1.
Could the above be done just with MySQL, or is it necessary to use PHP ? And if I need to use PHP what would be the approach ?
I'm not familiar with a pure MySQL way. Using PHP you'll select all id's from your table, and then in a foreach loop issue a CREATE TABLE user_specific_id + $id query
That being said, creating a separate table for each user doesn't sound like the correct way of handling a DB.
This sounds like an awfully bad idea.
It is a bad idea because you cannot decently JOIN the tables using mysql.
Instead of a table for each user, consider having a table with a multi-column primary key for all users.
You can, of course do what you described with PHP, for example using PDO.

Inserting in a table without primary key using Zend

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.

Categories