How to give where condition using for loop in Codeigniter - arrays

I have an array:
Array
(
[0] => 9055954
[1] => 2736738
[2] => 1234
[3] => 2844725
)
I need to a where condition to select the ids form database table add_family whose id is not equal to any one of the value in array, I'm using codeigniter, how can I check it using for loop for where condition.

You don't really need to run a for loop to do this. You can simply check it by running a simple query which will select all records from table after excluding ids as specified in array:
$ignore_ids = [9055954, 2736738, 1234, 2844725];
$this->db->where_not_in('add_family', $ignore_ids);

Related

Postgres - remove element from jsonb array

I have an array of jsonb elements (jsonb[]), with id and text. To remove an element I could use:
UPDATE "Users" SET chats = array_remove(chats, '{"id": 2, "text": "my message"')
But I want to delete the message just by the id, cause getting the message will cost me another query.
Assuming missing information:
Your table has a PK called user_id.
You want to remove all elements with id = 2 across the whole table.
You don't want to touch other rows.
id is unique within each array of chats.
UPDATE "Users" u
SET chats = array_remove(u.chats, d.chat)
FROM (
SELECT user_id, chat
FROM "Users", unnest(chats) chat
WHERE chat->>'id' = '2'
) d
WHERE d.user_id = u.user_id;
The following explanation matches the extent of provided information in the question:

INSERT+SELECT with a unique key

The following T-SQL statement does not work because [key] has to be unique and the MAX call in the SELECT statement only seems to be called once. In other words it is only incrementing the key value once and and trying to insert that value over and over. Does anyone have a solution?
INSERT INTO [searchOC].[dbo].[searchTable]
([key],
dataVaultType,
dataVaultKey,
searchTerm)
SELECT (SELECT MAX([key]) + 1 FROM [searchOC].[dbo].[searchTable]) AS [key]
,'PERSON' as dataVaultType
,[student_id] as dataVaultKey
,[email] as searchTerm
FROM [JACOB].[myoc4Data].[dbo].[users]
WHERE [email] != '' AND [active] = '1'
AND [student_id] IN (SELECT [userID] FROM [JACOB].[myoc4Data].[dbo].[userRoles]
WHERE ([role] = 'STUDENT' OR [role] = 'FACUTLY' OR [role] = 'STAFF'))
If you can make the key column an IDENTITY column that would probably be the easiest. That allows SQL Server to generate incremental values.
Alternatively, if you are definite about finding your own way to generate the key then a blog post I wrote last month may help. Although it uses a composite key, it shows you what you need to do to stop the issue with inserting multiple rows in a single INSERT statement safely generating a new value for each new row and it is also safe across many simultaneous writers (which many examples don't deal with)
http://colinmackay.co.uk/2012/12/29/composite-primary-keys-including-identity-like-column/
Incidentally, the reason that you get the same value for MAX(Key) on each row in your SELECT is that this happens at the time the table is read from. So for all the rows that the SELECT statement returns the MAX(key) will always be the same. Unless you add some sort of GROUP BY clause for any SELECT statement any MAX(columnName) function will return the same value for each row returned.
Also, all aggregate functions are deterministic, so for each equivalent set of input it will always have the same output. So if your set of keys was 1, 5, 9 then it will always return 9.

CakePHP update many to many resource

I have a category-product many to many relationship. When a product is created one or more categories are selected and then the product is saved. This works fine and the join table is correctly populated. The issue is when I go to edit the product, I can add more categories, change them etc... The issue comes when I try to save, it fails.
The line that I'm using to save is:
$this->Product->saveAll($this->data)
$this->Product->id is correctly populated and the debug of $this->data gives me an array like so :
Array(
[Product] => Array
(
[0] => 17
)
[Category] => Array
(
[0] => Array
(
[0] => 85
)
[1] => Array
(
[0] => 96
)
)
)
I don't know why it doesn't save as I can't find any detail on the error anywhere.
Any help much appreciated.
I think is this structure is wrong. If you are editing, where's the 'id' field of the product?
[Product] => Array
(
[id] => 17
)
I do that in an application. I will check it out tomorrow(today I cant view the source), but i think the id is the problem.
As raultm says, the structure was slightly wrong but it turned out to be validation in the model. The name and description fields were set to not allow empty and in the structure I wasn't passing these in (wasn't aware you had to on an update). By adding in the missing fields and naming the fields correctly in the array i.e. id, name and description, this worked.

What is the difference between save and saveAll function in cakephp?

can any one give example please
save is used to simply save a model:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Assuming the above information was stored in an array called $data, one would call
$this->ModelName->save($data);
in order to INSERT a record into the model's table (if id field is not specified) or UPDATE a record of the model's table (if id field is specified).
saveAll is used to:
Save multiple records of a model
Array
(
[Article] => Array
(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
So, you may save many models at the same time instead of looping and using save() each time.
Save related records of a model
Array
(
[User] => Array
(
[username] => billy
)
[Profile] => Array
(
[sex] => Male
[occupation] => Programmer
)
)
This would save both User and Profile models at the same time. Otherwise, you would have to call save() for User first, obtain the id of the newly saved user and then save Profile with user_id set to the obtained id.
Examples taken straight from the book.
saveAll saves all model data in a form, whereas save only saves one. So you would use save to save a single value, while saveAll basically saves you the trouble of using a loop for save.
As of Cake 2.0
save Saves model data (based on white-list, if supplied) to the
database. By default, validation occurs before save.
saveAll Saves multiple individual records for a single model; Also works with a single record, as well as all its associated records.

CakePHP array structure from a HABTM query

I have two tables linked via a HABTM. countries & networks. They are linked together by the countries_networks lookup table.
I need to get all of the countries and all of the associated network ids for each corresponding country. I don't need to go as far as getting the network names, simply getting the ids from the lookup table will suffice.
If I do a find(all) it gives me an array of countries but not in the structure that I need. I need to return something link this, but I only need Country.countryName and CountriesNetwork.network_id:
Array
(
[0] => Array
(
[Country] => Array
(
[countryName] => Aeroplane
)
[Network] => Array
(
[0] => Array
(
[id] => 1
[CountriesNetwork] => Array
(
[id] => 1
[country_id] => 1
[network_id] => 1
)
)
[1] => Array
(
[id] => 7
[CountriesNetwork] => Array
(
[id] => 2
[country_id] => 1
[network_id] => 7
)
)
)
)
)
Is there a way of doing this with a findall()? as if I pass fields as an array I always seem to get an unknown column names SQL error.
Or even a custom query? Many thanks in advance to anyone who may be able to help.
Pickledegg,
Sounds like you should investigate the Containable behavior. It will allow you to specify conditions on the joined models like you want, and will return the data formatted in the same manner as the normal find() call.
Check the manual for more information.
Since you have no model for network_countries it's not possible to get those values using find or findAll. I think the simpliest solution will be to run a custom query to retrieve the data you need.
$query = "SELECT country_id, network_id
FROM countries C LEFT JOIN network_countries NC ON (NC.country_id = C.id)"
$this->Country->query($query);
Unfortunately the result returned using that code will be formatted slightly different then find results.
Or you could add a model for network countries but I don't think this is a good idea.

Resources