i'm using NotOrm lib for working with data in the database. I have to follow the instructions herehttp://www.sitepoint.com/database-interaction-made-easy-with-notorm/
but did not solve my problem Here is my data structure:
Tables
-members
---Indexes
PRIMARY id
-cards
---Indexes
PRIMARY id
INDEX member_id
---Foreign keys
member_id members(id) CASCADE CASCADE
I used sql command
ALTER TABLE `cards` ADD FOREIGN KEY ( `member_id` ) REFERENCES `xxx`.`members` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE ;
my code
$cards = $this->db->cards();
foreach($cards as $c){
//echo "here";
echo $c->members["member_id"];
}
and response is
=> Message: Undefined index: members_id
Is my data structure correct for using NotORM ? i want to result from SQL as:
select members.f1, members.f2 from members join cards on members.id = cards.member_id where cards.email like '%%' or cards.phone like '%%'
Many thanks!
--
You received this message because you are subscribed to the Google Groups "NotORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to notorm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
/** Get value of column
* @param string column name
* @return string
*/
function offsetGet($key) {
$this->access($key);
if (!array_key_exists($key, $this->row)) {
$this->access($key, true);
}
return $this->row[$key];
}
$result = $this->db->members()->select('members.freepp_id')
->where('cards:is_property', 1)
->where('cards:status', 1)
->where('cards:email IN ? OR cards:mobile_phone IN ? OR cards:work_phone IN ? OR cards:home_phone IN ? OR cards:telephone IN ?', $email, $mobile_phone, $work_phone, $home_phone, $telephone);
echo $result;
$c = count($result);
echo $c;
var_dump($result->fetch());
if($data = $result->fetch()){
echo "HERE";
return $data['members.freepp_id'];
}
$email = array('do...@gmail.com', 'k...@gmail.com');
$mobile_phone = array('0934591929', '86676');
$work_phone = $home_phone = $telephone = array('');
$work_phone = $home_phone = $telephone = '';
public function test($email ){
$a = $this->db->cards()->select('members_id')
//->where("email IN (" . $this->db->cards()->select('email')->where('email IN ?', $email) . ")")
->where('email IN ?', $email);
//->where('is_property', 1)
//->where('status', 1);
echo $a;
$c = count($a);
echo 'count: ' . $c;
if($rs = $a->fetch()){
echo $rs['members_id'];
}
function stringHandling($str){
//$rsStr = '';
$strArr = explode(',', $str);
//for ($x = 0; $x < count($strArr); $x++) {
// if($x < count($strArr) - 1){
// $rsStr .= "'" . $strArr[$x] . "',";
// }else {
// $rsStr .= "'" . $strArr[$x] . "'";
// }
//}
//return "(" . $rsStr . ")";
return $strArr;
}
$op = $db->test(stringHandling($email));
echo $op;
$mobileArr = array('');
$a = $this->db->cards()->select('members_id')
->where("email",$emailArr);
$filter = $this->db->cards()->where('email', $email)->or('mobile_phone', $mobile_phone)->or('work_phone', $work_phone)->or('home_phone', $home_phone)->or('telephone', $telephone);
$result = $this->db->members()->select('members.freepp_id')->where('cards:is_property', 1)->where('cards:is_property', 1)->where('cards:status', 1)->where('cards:id', $filter);
$filter = $this->db->cards();
if(count($email))
$filter->or('email', $email);
if(count($mobile_phone))
$filter->or('mobile_phone', $mobile_phone);
if(count($work_phone))
$filter->or('work_phone', $work_phone);
if(count($home_phone))
$filter->or('home_phone', $home_phone);
if(count($telephone))
$filter->or('telephone', $telephone);
for($x = 0; $x < count($emailArr); $x++){
$card->or('email LIKE ? ', "%" . $emailArr[$x] . "%)");
}
$card->where("is_property", 1)->and("status", 1);
echo $card;
$filter = $db->cards();
foreach($emailArr as $email)
$filter = $filter->or("email LIKE ?", "%".$email."%");
$result = $db->cards()->where("id", $filter)->and("is_property", 1)->and("status", 1)->count();
edit | id | members_id | status | is_property | mobile_phone | work_phone | home_phone | |
---|---|---|---|---|---|---|---|---|
edit | 3 | 1 | hiepnd...@gmail.com | 1 | 1 | NULL | NULL | NULL |
edit | 4 | 4 | t...@gmail.com,do...@gmail.com,hiepnd...@gmail.com | 1 | 1 | 868686868,000909 | NULL | NULL |
edit | 5 | 5 | nghiada...@gmail.com | 1 | 1 | NULL | NULL | NULL |
edit | 6 | 5 | hiepnd...@gmail.com | 0 | 0 | 88888#000909 | NULL | NULL |
$email = "";
foreach($emailArr as $email)
$email .= "email LIKE '%$email%' ");
$result = $db->cards()->where($email)->and("is_property", 1)->and("status", 1)->count("*");
--
$result = $this->db->cards()
->where("id", $emailFilter)
->or("id", $mobilePhoneFilter)
->or("id", $workPhoneFilter)
->or("id", $homePhoneFilter)
->or("id", $telePhoneFilter)
->and("is_property", 1)
->and("status", 1)
->count("*");
edit | id | members_id | first_name | status | is_property |
---|---|---|---|---|---|
edit | 3 | 1 | Ku | 1 | 1 |
edit | 4 | 4 | android2 | 1 | 1 |
edit | 5 | 5 | NULL | 1 | 1 |
edit | 6 | 5 | nghia | 0 | 0 |
edit | id | cards_id | |
---|---|---|---|
edit | 1 | 92 | do...@gmail.com |
edit | 2 | 92 | lum...@gmail.com |
edit | 3 | 92 | no...@gmail.com |
edit | 4 | 4 | do...@gmail.com |
$filter = $this->db->cards();
if(count($email))
$filter->or('email', $email);
if(count($mobile_phone))
$filter->or('mobile_phone', $mobile_phone);
if(count($work_phone))
$filter->or('work_phone', $work_phone);
if(count($home_phone))
$filter->or('home_phone', $home_phone);
if(count($telephone))
$filter->or('telephone', $telephone);
$result = $db->cards()->where("id", $filter)->and("is_property", 1)->and("status", 1)->count("*");
$values = array(
'id' => NULL, // omit this line or NULL for AUTO_INCREMENT 'members_id' => 1,
'first_name' => 'Ku',
'status' => '1',
'is_property' => '1'
);
$result = $db->cards()->insert($values);
if($result !== false){ // if not error, $result has all of the new inserted row data
$inserted_card_id = $result['id'];
$values = array( // ID column was ommited for AUTO_INCREMENT
'cards_id' => $inserted_card_id, );
$db->emails_card()->insert($values);
}
id | members_id | is_property | status | |
---|---|---|---|---|
61 | do...@gmail.com | 9 | 1 | 1 |
69 | 9 | 0 | 0 | |
70 | 9 | 0 | 0 | |
71 | h...@vdc.com.vn | 9 | 0 | 0 |
72 | Roj...@gmail.com | 9 | 0 | 0 |
73 | nguyent...@baoviei.com.vr | 9 | 0 | 0 |
76 | ha...@gmail.com | 9 | 0 | 0 |
77 | ha...@gemtek.com | 9 | 0 | 0 |
SELECT cards.id, cards.members_id,cards.first_name, cards.last_name, emails_card.emailFROM cards LEFT JOIN emails_card ON cards.id = emails_card.cards_id LEFT JOIN mobiles_card ON cards.id = mobiles_card.cards_id LEFT JOIN work_phones_card ON cards.id = work_phones_card.cards_id LEFT JOIN home_phones_card ON cards.id = home_phones_card.cards_id LEFT JOIN telephones_card ON cards.id = telephones_card.cards_id WHERE cards.members_id=38 and cards.id=957
id | members_id | first_name | last_name | |
---|---|---|---|---|
957 | 38 | gefgg | ij...@gmail.com | |
957 | 38 | gefgg | ok...@gmail.com |
function getCardList($mid, $db){
$res = $db->cards()->where('cards.members_id', $mid);
foreach($res as $card){
$arr[$card['id']]['emails'] = array_map(
'iterator_to_array',
iterator_to_array(
$db->emails_card()->where('cards_id', $card['id']),
false
)
);
$arr[$card['id']]['mobiles'] = array_map(
'iterator_to_array',
iterator_to_array(
$db->mobiles_card()->where('cards_id', $card['id']),
false
)
);
}
return json_encode($arr);
}
{"emails":[{"id":"1","cards_id":"1","email":"a...@a.com"},{"id":"2","cards_id":"1","email":"b...@a.com"}],"mobiles":[]}
function getCardList($mid, $db){
$res = $db->cards()->where('cards.members_id', $mid);
foreach($res as $card){
$arr[$card['id']]['emails'] = array_map(
function($item){
return $item['email'];
},
iterator_to_array(
$db->emails_card()->where('cards_id', $card['id'])->select('email'),
false
)
);
$arr[$card['id']]['mobiles'] = array_map(
function($item){
return $item['mobile_phone'];
},
iterator_to_array(
$db->mobiles_card()->where('cards_id', $card['id'])->select('mobile_phone'),
false
)
);
}
return json_encode($arr);
}
And the output would be something like this:
{"1":{"emails":["a...@a.com","b...@a.com"],"mobiles":[]},"2":{"emails":["a...@b.com","b...@b.com"],"mobiles":[]}}The differences where the ->select() in the inner query and the outer 'iterator_to_array' in array_map() was changed to a custom function that instead of converting the whole row to an array just returns the column you want.In my opinion, this second version should be better.
{"1":{"emails":[{"id":"1","cards_id":"1","email":"a...@a.com"},{"id":"2","cards_id":"1","email":"b...@a.com"}],"mobiles":[]},"2":{"emails":[{"id":"3","cards_id":"2","email":"a...@b.com"},{"id":"4","cards_id":"2","email":"b...@b.com"}],"mobiles":[]}}
[{"id":"1","first_name":"first_name_a","last_name":"last_name_a","emails":[{"email":"a...@a.com"},{"email":"b...@a.com"}],"mobiles":[{"mobile_phone":"1111"}]},{"id":"2","first_name":"first_name_b","last_name":"last_name_b","emails":[{"email":"a...@b.com"},{"email":"b...@b.com"}],"mobiles":[{"mobile_phone":"2222"}]}]
function getCardList($mid, $db){
$res = $db->cards()->where('cards.members_id', $mid);
$cards_columns = array('id', 'first_name', 'last_name');
$emails_columns = array('email');
$mobiles_columns = array('mobile_phone');
$arr = array();
foreach($res as $card){
$arr_ = convert_notorm_row_to_array($card, $cards_columns);
$emails_query = $db->emails_card()->where('cards_id', $card['id']);
$arr_['emails'] = convert_notorm_result_to_array($emails_query, $emails_columns);
$mobiles_query = $db->mobiles_card()->where('cards_id', $card['id']);
$arr_['mobiles'] = convert_notorm_result_to_array($mobiles_query, $mobiles_columns);
$arr[] = $arr_;
}
return json_encode($arr);
}
function convert_notorm_row_to_array($row, $columns = array()){
$arr = iterator_to_array($row);
if(is_array($columns) && !empty($columns))
foreach($arr as $k => $v)
if(!in_array($k, $columns))
unset($arr[$k]);
return $arr;
}
function convert_notorm_result_to_array($res, $columns = array()){
return array_map(
function($item) use ($columns) {
return convert_notorm_row_to_array($item, $columns);
}, iterator_to_array($res, false)
);
}
$res = $db->cards()->where('cards.members_id', $mid);
To setup conditions for the email, mobiles, ... you (which I think you shouldn't, since the conditions will probably be set in the card selection) you can do it in these lines:
...$emails_query = $db->emails_card()->where('cards_id', $card['id']);...
$mobiles_query = $db->mobiles_card()->where('cards_id', $card['id']);...
...
$cards_columns = array('id', 'first_name', 'last_name');
$emails_columns = array('email');
$mobiles_columns = array('mobile_phone');
...
$arr_ = convert_notorm_row_to_array($card, $cards_columns);...
$arr_['emails'] = convert_notorm_result_to_array($emails_query, $emails_columns);...
$arr_['mobiles'] = convert_notorm_result_to_array($mobiles_query, $mobiles_columns);...
$emails_columns = array('email');
$emails_query = $this->db->emails_card()->where('cards_id', 1070);
function convert_notorm_result_to_array($emails_query, $columns = array()){
return array_map(
function($item) use ($columns) {
return $this->convert_notorm_row_to_array($item, $columns);
}, iterator_to_array($emails_query, false)
);
}
$emails_query = $this->db->emails_card()->where('cards_id', $card['id']);
echo $emails_query;
$arr_['emails'] = $this->convert_notorm_result_to_array($emails_query, $emails_columns);
function($item) use ($columns) { <----- Is there a problem in here ?
error_reporting(E_ALL);
ini_set('display_errors', 'On');
return $this->convert_notorm_row_to_array($item, $columns);
return convert_notorm_row_to_array($item, $columns);
foreach($res as $card){
$arr = $this->convert_notorm_row_to_array($card, $cards_columns);
print_r($arr);
}
public function test($mid){
$res = $this->db->cards()->where('cards.members_id', $mid);
$cards_columns = array('id', 'first_name', 'last_name', 'address');
$emails_columns = array('cards_id','email');
$arr = array();
foreach($res as $card){
$arr = DbHandler::convert_notorm_row_to_array($card, $cards_columns);
$emails_query = $this->db->emails_card()->where('cards_id', $card['id']);
$arr_['emails'] = DbHandler::convert_notorm_result_to_array($emails_query, $emails_columns);
$arr[] = $arr_;
}
return json_encode($arr);
}
public static function convert_notorm_row_to_array($row, $columns = array()){
$arr = iterator_to_array($row);
if(is_array($columns) && !empty($columns))
foreach($arr as $k => $v)
if(!in_array($k, $columns))
unset($arr[$k]);
return $arr;
}
public static function convert_notorm_result_to_array($res, $columns = array()){
return array_map(
function($item) use ($columns) {
return DbHandler::convert_notorm_row_to_array($item, $columns);
}, iterator_to_array($res, false)
);
}
{ "id": "1122", "first_name": "Thuy", "last_name": "", "address": "Nguyen Trai Hanoi Vietnam ", "0": { "emails": [ { "cards_id": "1122", "email": "thuy....@gmail.com" } ] } }
edit | 692 | 1074 | thuy....@gmail.com |
edit | 693 | 1075 | th...@gmail.com |
$arr = DbHandler::convert_notorm_row_to_array($card, $cards_columns);
$arr_ = DbHandler::convert_notorm_row_to_array($card, $cards_columns);
{
"id": "1122",
"first_name": "Thuy",
"last_name": "",
"address": "Nguyen Trai Hanoi Vietnam ",
"0": {
"emails": [
{
"cards_id": "1122",
"email": "thuy....@gmail.com"
}
]
}
}
SELECT * FROM cards WHERE (members_id = 35)
id members_id first_name last_name address fax job website photo avatar photo_other_card note created modified group_name status is_property 1032 35 NULL NULL NULL NULL NULL NULL NULL 1 1 1059 35 CSKH NULL NULL NULL 2015-04-02 14:26:17 NULL 0 0 1060 35 Em Thắm NULL NULL NULL 2015-04-02 14:26:18 NULL 0 0 1061 35 Hoá Androi NULL NULL NULL 2015-04-02 14:26:18 NULL 0 0 1070 35 .1. M. .l| ,.'xHHfl. åœ°å €å’€ä½£è§¦åŸºéš†å¸‚æš–æš–åŒ¾æš–æš–è¡—åˆ ç ¥6楼 NULL NULL 2015-04-02 14:50:50 NULL 0 0 1071 35 Giang Me Tri Ha Ha Noi Vietnam NULL NULL 2015-04-02 14:50:51 NULL 0 0 1072 35 Giang Me Tri Ha Ha Noi Vietnam NULL NULL 2015-04-02 14:50:56 NULL 0 0 1073 35 Linh Nguyen Ba LaNguyen Trai Ha Noi Vietnam NULL NULL 2015-04-02 14:50:56 NULL 0 0 1074 35 Thuy Nguyen Trai Hanoi Vietnam NULL NULL 2015-04-02 14:50:57 NULL 0 0 1075 35 Thuy Nguyen Trai Hanoi Vietnam NULL NULL 2015-04-02 14:50:58 NULL 0 0
hi,
...
{ "status": 1, "card_list": [ { "id": "2258", "first_name": "Pú Tin", "last_name": null, "fax": null, "job": null, "company": null, "website": [ "abc.com", "123.vn" ], "emails": [ { "email": "do...@gmail.com" } ], "mobiles": [], "home_phones": [], "work_phones": [], "telephone_phones": [], "other_phones": [] }, { "id": "2264", "first_name": "Blackberry", "last_name": null, "fax": null, "job": null, "company": null, "website": [ "" ], "emails": [ { "email": "black...@gmail.com" }, { "email": "b...@gmail.com" } ], "mobiles": [], "home_phones": [], "work_phones": [], "telephone_phones": [], "other_phones": [] } ]}
"emails": [
"a...@a.com",
"b...@a.com"
]
public function getCardList($mid){
$res = $this->db->cards()->where('cards.members_id', $mid)->and('is_property', 0);
$cards_columns = array('id', 'first_name', 'last_name', 'job', 'company', 'fax');
$emails_columns = array('email');
$mobiles_columns = array('mobile_phone');
$home_phones_columns = array('home_phone');
$work_phones_columns = array('work_phone');
$telephones_columns = array('telephone');
$other_phones_columns = array('other_phones');
$website_columns = array('website');
$address_columns = array('address');
$arr = array();
foreach($res as $card){
$arr_ = DbHandler::convert_notorm_row_to_array($card, $cards_columns);
$arr_['website'] = explode(',', DbHandler::dissection_notorm_row($card, $website_columns));
$arr_['address'] = explode(',', DbHandler::dissection_notorm_row($card, $address_columns));
//((is_array($arr_['website']) && !empty($arrTmp)) ? $arr_['website'] : '[]');
$emails_query = $this->db->emails_card()->where('cards_id', $card['id']);
$arr_['emails'] = DbHandler::convert_notorm_result_to_array($emails_query, $emails_columns);
$mobiles_query = $this->db->mobiles_card()->where('cards_id', $card['id']);
$arr_['mobiles'] = DbHandler::convert_notorm_result_to_array($mobiles_query, $mobiles_columns);
$home_phones_query = $this->db->home_phones_card()->where('cards_id', $card['id']);
$arr_['home_phones'] = DbHandler::convert_notorm_result_to_array($home_phones_query, $home_phones_columns);
$work_phones_query = $this->db->work_phones_card()->where('cards_id', $card['id']);
$arr_['work_phones'] = DbHandler::convert_notorm_result_to_array($work_phones_query, $work_phones_columns);
$telephone_query = $this->db->telephones_card()->where('cards_id', $card['id']);
$arr_['telephone_phones'] = DbHandler::convert_notorm_result_to_array($telephone_query, $telephones_columns);
$other_phones_query = $this->db->other_phones_card()->where('cards_id', $card['id']);
$arr_['other_phones'] = DbHandler::convert_notorm_result_to_array($other_phones_query, $other_phones_columns);
$arr[] = $arr_;
}
return $arr;
}
public static function dissection_notorm_row($row, $columns = array()){
$arr = iterator_to_array($row);
$value = '';
if(is_array($columns) && !empty($columns))
foreach($arr as $k => $v)
if(in_array($k, $columns))
$value = $v;
return $value;
}
/**
* @param $row
* @param array $columns
* @return array
*/
public static function convert_notorm_row_to_array($row, $columns = array()){
$arr = iterator_to_array($row);
//print_r($arr);
if(is_array($columns) && !empty($columns))
foreach($arr as $k => $v)
if(!in_array($k, $columns))
unset($arr[$k]);
return $arr;
}
/**
* @param $res
* @param array $columns
* @return array
*/
public static function convert_notorm_result_to_array($res, $columns = array()){
return array_map(
function($item) use ($columns) {
return DbHandler::convert_notorm_row_to_array($item, $columns);
}, iterator_to_array($res, false)
);
}
public function getCardList($mid){ $res = $this->db->cards()->where('cards.members_id', $mid)->and('is_property', 0); // <--- getCardList by mId ( is_property = 0 ) $cards_columns = array('id', 'first_name', 'last_name', 'job', 'company', 'fax', 'status', 'freepp_id');
$emails_columns = array('email'); $mobiles_columns = array('mobile_phone'); $home_phones_columns = array('home_phone'); $work_phones_columns = array('work_phone'); $telephones_columns = array('telephone'); $other_phones_columns = array('other_phones');
$website_columns = array('website'); $address_columns = array('address');
$arr = array();
foreach($res as $card){ $arr_ = DbHandler::convert_notorm_row_to_array($card, $cards_columns);
$website_query = $this->db->websites_card()->where('cards_id', $card['id']); $arr_['website'] = DbHandler::convert_notorm_result_to_array($website_query, $website_columns);
$address_query = $this->db->address_card()->where('cards_id', $card['id']); $arr_['address'] = DbHandler::convert_notorm_result_to_array($address_query, $address_columns);
$emails_query = $this->db->emails_card()->where('cards_id', $card['id']); $arr_['emails'] = DbHandler::convert_notorm_result_to_array($emails_query, $emails_columns);
$mobiles_query = $this->db->mobiles_card()->where('cards_id', $card['id']); $arr_['mobiles'] = DbHandler::convert_notorm_result_to_array($mobiles_query, $mobiles_columns);
$home_phones_query = $this->db->home_phones_card()->where('cards_id', $card['id']); $arr_['home_phones'] = DbHandler::convert_notorm_result_to_array($home_phones_query, $home_phones_columns);
$work_phones_query = $this->db->work_phones_card()->where('cards_id', $card['id']); $arr_['work_phones'] = DbHandler::convert_notorm_result_to_array($work_phones_query, $work_phones_columns);
$telephone_query = $this->db->telephones_card()->where('cards_id', $card['id']); $arr_['telephone_phones'] = DbHandler::convert_notorm_result_to_array($telephone_query, $telephones_columns);
$other_phones_query = $this->db->other_phones_card()->where('cards_id', $card['id']); $arr_['other_phones'] = DbHandler::convert_notorm_result_to_array($other_phones_query, $other_phones_columns);
$arr[] = $arr_; }
return $arr; }