Home > Backend Development > PHP Tutorial > yii2 添加城市功能save()问题

yii2 添加城市功能save()问题

WBOY
Release: 2016-06-06 20:19:57
Original
1527 people have browsed it

我想要添加该省份下的城市和改城市对应的级别(A,B,C,D,E),省份和级别都是字符串,但城市可能是一个或多个一个的话就是字符串,多个就是数组,如果为数组改怎么添加数据库(如果有两个城市就要添加两条记录,如 江苏省,苏州市,B ; 江苏省,南京市,B),并且所有数据插入成功要返回1,有一条失败就返回0,求解决

<code>public function add($post){
    $province = $post['province'];
    $citys = $post['city'];
    $city_level = $post['city_level'];
    var_dump($citys);exit;
    $this->province = $province;
    $this->city_level = $city_level;
    foreach ($citys as $city){
        $this->city = $city;
        if($this->save()){
            return 1;
        }else{
            return 0;
        }
    }
}</code>
Copy after login
Copy after login

回复内容:

我想要添加该省份下的城市和改城市对应的级别(A,B,C,D,E),省份和级别都是字符串,但城市可能是一个或多个一个的话就是字符串,多个就是数组,如果为数组改怎么添加数据库(如果有两个城市就要添加两条记录,如 江苏省,苏州市,B ; 江苏省,南京市,B),并且所有数据插入成功要返回1,有一条失败就返回0,求解决

<code>public function add($post){
    $province = $post['province'];
    $citys = $post['city'];
    $city_level = $post['city_level'];
    var_dump($citys);exit;
    $this->province = $province;
    $this->city_level = $city_level;
    foreach ($citys as $city){
        $this->city = $city;
        if($this->save()){
            return 1;
        }else{
            return 0;
        }
    }
}</code>
Copy after login
Copy after login

数据库其实设计的不好:

id parent_id name
1 0 北京
2 1 海淀
3 1 朝阳

用一个 parent_id 来控制层级关系即可

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template