Using Laravels Eloquent with database views - php

I have two database tables, expenses and incomes and I created a MySQL view, transactions, which basically is an UNION between the two tables, selecting the fields I am interested in.
The transactions view contains the following columns:
transaction_id
user_id
amount
note
transaction_type
updated_at
created_at
I created an Eloquent Model for the view, called Transactions.
The problem is when I want to retrieve transactions based on certain criteria.
E.g. I want to get all transactions for a User. Normally, if transactions was a table, I would define a foreign key relationship and I would simply call $user->hasMany('App\Models\Transaction').
Since foreign keys are not possible for views, I tried using the 'where' method: Transaction::where('user_id', $user->id). This query does not return anything, neither do any other queries I tested. The only method that returned data is Transaction::all(), but this doesn't help me very much.
What am I doing wrong?

For where statement in Eloquent you must to end up with get, try this:
$transactions = Transaction::where('user_id', $user->id)->get();

Related

Quickbooks Queuing

Currently i have a customer form which inserts data into multiple tables.
I am integrating with QuickBooks PHP-API (Consolibyte).
When the data is inserted following is the queuing method that proceeds after insertion in a table.
$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER, $id);
where $id is the last_insert_id() in a table.
Since there are entries happening to multiple tables, how do i update Customer records in Quickbooks by fetching data from multiple table since each table will return its own last insert id.
Since there are entries happening to multiple tables`
Presumably, you have a common customer_id field or something that is shared across all tables.
Use that customer_id value.
how do i update Customer records in Quickbooks by fetching data from multiple table
SQL supports something called JOINs which are specifically meant to allow you to "smush" two related tables together and get all the data related to your specific customer from all of the related tables.
Use a JOIN. Get all the data from the tables.
Build your qbXML request from that.

PHP implementation of n:m relationship

As to implement a N:M relationship of tables USERS (PK: user_id which is auto increment INT) and REQUESTS(PK: request_id which is auto increment INT), I've created the intermediate table USERS_GROUPS (PKs: two FKs US_userid, US_requestid which coincide with the PKs of USERS and REQUESTS). When a user creates a new request I have to create a new record inside REQUESTS.
INSERT INTO REQUESTS (...) VALUES (...);
At the same time I want to create a new record inside USERS_GROUPS as to join USERS_GROUPS with USERS and REQUESTS. The value of the first FK pointed to USERS is known to me, however how may I find the value of the second FK pointed to the record I've just created?
In other words, I want to find the value of the field request_id of the record I've just created. This seems a little bit confusing to me and I don't know how to implement it.
INSERT INTO USERS_GROUPS (US_userid,US_request_id) VALUES (...,?????);
This is my first serious db schema I've ever created and my first n:m relationship I have to manage. Is my point of view correct? If yes, I 'd appreciate your help in how to realise it. If not which is the right approach?
You can use last-insert-id to retrieve the newly created id.
The library you use to connect to database usually has a function for it, such as mysqli_insert_id or PDO::lastInsertId.
Sidenote: Because you are performing multiple (independent) queries on the database it may be necessary to wrap it all in transaction.

Doctrine find related records without foreign keys

I have two preexisting tables that were created by a CMS. I've mapped them in Symfony2 and I am trying to establish a relationship between two of the tables but I am now noticing that the CMS created these tables without a foreign key. This table table_uploads has a column that every other table is related to called column_table_name and column_record_num.
So assuming we have three tables... table_students, table_uploads, and table_teachers, the table table_uploads will have a value of either students or teachers in column_table_name to show its relation to the other two tables as well as a number in column_record_num that corresponds to the other table's primary key. Neither of the three tables actually have a defined foreign key however.
In Symfony, I'd like to call a table
$students = $this->getDoctrine()->getRepository('SchoolBundle:Students')->findAll();
and then load the related records from another table in such a way like (I know select_related doesn't actually exist, but this is essentially the functionality I'm looking to create)
$students->select_related()
Is this at all possible without the existence of a foreign key? The only way I can think of doing it is grabbing the column values from $students and running another query to other table with these values and merging the two results.
You could create a custom repository method that would join unrelated tables. DQL join between unrelated entities?

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

getArrayResult on entity with ManyToOne association

Having the follow basic tables (one-to-many relationship)
Client - Has many users.
Users - Each user belongs to single client.
In a very simple example if I query the user entity (Querybuilder)
with getArrayResult() I see the following:
The actual generated SQL contains the foreign key field to be
returned (i.e. ClientID)
The actual returned data array does NOT contain the foreign key
field.
At this stage I do not need to return foreign data and so do not need
to join to the associated table.
So question is...
What or how do I return the foreign key value in my array?
Query is:
$qb = $this->_em->createQueryBuilder();
$qb->select('e');
$qb->from('Entity\User', 'e');
SQL is:
SELECT w0_.Id AS Id0, w0_.Name AS Name2, w0_.ClientID AS ClientID7
FROM users w0_
Try to set the HINT_INCLUDE_META_COLUMNS query hint on the query (not the builder) before you execute it.
$q->setHint(Query::HINT_INCLUDE_META_COLUMNS, true);
As far as I know, you can't do this, because ClientID is not a property of User. User has a property like $client, and that client entity has an $id.
This is confusing you because you're dealing with Entites but you're still thinking in SQL.
The solution, though slightly less efficient, would probably be to join the Client entity into your DQL query, and then get $results[N]['client']['id'] (or similar, I'm not too familiar with getResultArray())

Categories