2010-01-14 87 views
1

这是我第一次尝试使用MySQL中的关系数据库和使用CodeIgniter在PHP中创建的某种CMS来认真对待事情。
我来到了我不得不在少数多对多表中插入一些数据的部分。
在我的代码一切正常(良好,测试仅仅几分钟),但我需要一些帮助创建可重用的功能,这将帮助我很多事情,使我在表中的所有关系......帮助创建可动态调用其他函数的可重用PHP函数

我试着评论在我的代码一切,所以所有的解释都在那里......

<?php 
function add(){ 

// This is what user posted in form. 
// There are two input fields: 
// name is always only one record 
// country can be a single record or array separated with " | " characters 
// I use CodeIgniter's $this->input->post instead of $_POST[] 
$name = $this->input->post('name'); 
$countries = $this->input->post('country'); 

// Inserting data to first table 
$data = array('firstName' => htmlentities($name)); // preparing array for inserting 
$insert_name = $this->db->insert('names', $data); // inserting with CodeIgniter's help 
$last_inserted_ID = $this->db->insert_id(); // getting last inserted ID 

// Inserting data to second table 

// Formatting of posted string of countries 
// Users can post strings similar to this: 
// "Austria" 
// "Austria |" 
// "Austria | " 
// "Austria | Australia" 
// "Austria | Australia |" 
// "Austria | Australia | " 
// and similar variations 
// What I need here is clear array with country names 
$separator = strpos($countries,"|"); // check for "|" character 
if ($separator === FALSE){ // if there is no "|" character in string 
    $countries_array[] = $countries; // array is only one value (only one country) 
} else { 
    $countries_array = explode(" | ", $countries); // explode my array 
    if (end($countries_array) == ""){ // if last item in array is "" 
    array_pop($countries_array); // eliminate last (empty) item 
    } 
} 

// Now, this is the part I think I will use lots of times. 
// I would like to make this a separate function so I could use it in many places :) 
// I would pass to that function few values and I would use one of them 
// to call different functions in this same class. 
// I guess I should pass data ($countries_array) and function names I wish to call?????? This is problematic part for my brain :)) 
// Check the comments below... 
for ($i = 0; $i < sizeof($countries_array); $i++){ 
    $insertIDS = array(); // this will be an array of IDs of all countries 
    $tempdata = $this->get_countries($countries_array[$i]); // query which looks if there is a country with specific name 
       // Right here, instead of calling $this->get_countries 
       // I would like to call different functions, for example 
       // $this->get_links($links_array[$i]) 
       // or $this->get_categories($categories_array[$i]) 
       // etc. 
    if(sizeof($tempdata) != 0){ // so, if a record already exists 
    foreach ($tempdata as $k => $v){ 
    $insertIDS[] = $k; // insert those IDs in our array 
    } 
    } else { // and if a record does not exist in db 
    $this->add_country($countries_array[$i]); // add it as a new record... 
       // This is also one of the places where I would call different functions 
       // for example $this->add_link($links_array[$i]) 
       // or $this->add_categories($categories_array[$i]) 
       // etc. 
    $insertIDS[] = $this->db->insert_id(); // ...get its ID and add it to array 
    } 

    // Finally, insert all IDs into junction table! 
    foreach ($insertIDS as $idKey => $idValue){ 
    $this->add_names_countries($last_inserted_ID, $idValue); // Another place for calling different functions 
        // example $this->add_names_links($last_inserted_ID, $idValue) 
        // etc. 
    } 
} 

} 
?> 

好了,看着现在这个代码,我看到,我可以把那部分的格式也该功能,但没有这么多现在重要...

非常感谢您的任何帮助!

回答

3

这样做的首选方法是使用Table Data Gateway。取而代之的

$this->db->insert('countries', $data); 

您在数据库中创建的每个表类。每个表将CRUD逻辑封装到类中,例如,

class Countries 
{ 
    $protected $_db; 

    public function __construct($db) 
    { 
     $this->_db = $db; 
    } 

    public function save(array $countries) 
    { 
     $this->db->insert('countries', $countries); 
    } 

    // ... other methods 
} 

另外,我建议使用transactions对于这样的工作,因为所有的东西属于一起,你可能不想插入任何数据,如果查询的一个失败。我不知道CodeIgnitor如何处理事务,但基本上,你应该这样做则是这样的:

$this->db->startTransaction();   // like try/catch for databases 
$countries = new Countries($this->db); 
$countries->save($countryData); 
$links = new Links($this->db); 
$links->save($linkData); 
// ... 
if($this->db->commit() === false) {  // returns true when no errors occured 
    $this->db->rollback();    // undos in case something went wrong 
} 

虽然这不会回答你的问题如何动态地调用一个函数(call_user_func()能做到这一点),这样做像上面提出的那样,使你的代码更易于维护。

你的问题有点含糊不清,如果你想运行一个序列中的所有功能,或者只是想根据用户提交的内容进行交换。对于第一种情况,使用交易方式。对于第二种情况,您只需实例化适当的类并调用save方法。

+0

谢谢!这看起来很有趣,我不知道表数据网关的东西:)我会尽力实现它,但为此答案+1! :) – errata 2010-01-14 11:46:08

+0

辉煌!非常感谢你为这个超级有用的信息!我一定会尝试研究这种方法并使用它,因为我想坚持使用OOP :)我会接受这个作为最终答案,并发布另一个问题,如果我被困在这个:)谢谢你百万次! :)) – errata 2010-01-14 12:19:27

2

的不完全确定你的要求,但我想你可能是call_user_func后:

function process($countries) { 
// do stuff 
} 

$function_name = 'process'; 

call_user_func($function_name, $countries); 

这样一来,就可以动态地分配基础上,比如说一个功能,国家的名单。

+0

谢谢亚当,我想这正是我心中的想法!我会尽力实现戈登和你的榜样,但我认为这个答案也适用于+1! :) – errata 2010-01-14 11:47:37