php 使用CodeIgniter DataMapper ORM保存一对一关系

s4chpxco  于 2023-03-07  发布在  PHP
关注(0)|答案(1)|浏览(82)

我的数据库架构:

CREATE TABLE IF NOT EXISTS `customers` 
(
    `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
    `person_id` smallint(5) unsigned NOT NULL,
    `status` tinyint(1) unsigned NOT NULL DEFAULT 1,
    PRIMARY KEY (`id`),
    KEY `fk_customers_persons_idx` (`person_id`)
);

CREATE TABLE IF NOT EXISTS `persons` 
(
    `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(64) NOT NULL,
    `phone` char(10) DEFAULT NULL,
    `mobile` char(10) DEFAULT NULL,
    `email` varchar(64) NOT NULL,
    `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `data_updated` timestamp NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
);

下面是我的控制器代码:

class Test extends CI_Controller {
    public function index()
    {
        // Faking POST values
        $_POST = array(
            // Person info
            'name' => 'Paulo Freitas',
            'phone' => 'xxxxxxxxxx',
            'email' => 'xx@xxxxxxxxxxxx.xx',
            // Customer info
            'status' => 2
        );

        // Utility function
        function factory_from($class, $fields)
        {
            $CI =& get_instance();
            $input = array();

            foreach ($fields as $field) {
                $input[$field] = $CI->input->post($field) ?: null;
            }

            $obj = new $class;
            $obj->from_array($input);

            return $obj;
        }

        // Save person
        $person = factory_from('Person', array(
            'name',
            'phone',
            'mobile',
            'email'
        ));
        $person->save();
        // Save customer
        $customer = factory_from('Customer', array(
            'status'
        ));
        $customer->save($person);
        var_dump($customer->id); // New customer id
    }
}

我刚接触CodeIgniter的DataMapper ORM,对于如何确保仅在成功存储相关客户时才存储人员有点困惑。例如,如果我验证了客户status,但失败了,为了存储该客户,我以前必须存储其相关人员...如果无法存储该客户,我如何回滚新人员?(在实际场景中,我有personsindividualsuserscustomers表,只有在所有表都成功时才需要存储它们)
我如何在这里使用事务?是的,我已经阅读了关于使用事务的文档,但是我不明白,到现在为止我已经被困在那里几个小时了。提前感谢你!

    • 更新**

我黑了我的控制器一点,现在它似乎工作,有没有更好的方法来实现这一点?
新控制器:

class Test extends CI_Controller {
    public function index()
    {
        // Faking POST values
        $_POST = array(
            // Person info
            'name' => 'Paulo Freitas',
            'phone' => 'xxxxxxxxxx',
            'email' => 'xx@xxxxxxxxxxxx.xx',
            // Customer info
            'status' => 2
        );

        // Utility functions
        function factory_from($class, $fields)
        {
            $CI =& get_instance();
            $input = array();

            foreach ($fields as $field) {
                $input[$field] = $CI->input->post($field) ?: null;
            }

            $obj = new $class;
            $obj->from_array($input);

            return $obj;
        }

        function get_errors()
        {
            $errors = array();

            foreach (func_get_args() as $obj) {
                $errors += $obj->error->all;
            }

            return $errors;
        }

        // Initialize person
        $person = factory_from('Person', array(
            'name',
            'phone',
            'mobile',
            'email'
        ));
        // Initialize customer
        $customer = factory_from('Customer', array(
            'status'
        ));

        // Start transaction
        $person->trans_begin();

        if ($person->save()
                && $customer->save($person)) {
            // If we can save all data, commit!
            $person->trans_commit();

            // Dump new customer id
            var_dump($customer->id);
        } else {
            // Otherwise, rollback all!
            $person->trans_rollback();

            // Dump all errors
            var_dump(get_errors($person, $customer));
        }
    }
}
cyvaqqii

cyvaqqii1#

// Begin transaction
$p->trans_begin();

// Attempt to save person
$p->save();

// Check status of transaction
if ($p->trans_status() === FALSE)
{
// Transaction failed, rollback
$p->trans_rollback();

// Add error message
$u->error_message('transaction', 'The transaction failed to save (insert)');
}
else
{

//since the person has been saved, then we can now process the customer

// Begin transaction
$c->trans_begin();

// Attempt to save person
$c->save($p);

if( $c->trans_status === TRUE ){

 $p->commit(); 
 $c->commit();

}

}

/*
 * you can nest a lot of if statements depending on the number of objects you want to save
 * I followed whats exactly in the documentation for that. So give it a try, it should run.
 * dont forget to create $c and $p as datamapper objects.
 */

相关问题