抓取然后插入数据库

时间:2014-04-29 11:58:02

标签: php yii simple-html-dom

我是yii框架的新手,我在yii中有这个代码,我尝试插入到数据库但没有什么保存到db为什么?

    public function actionAhmad() {
    $connection = yii::app()->db;
    $UserModel = new User();
    $AdverModel = new Adver();
    $html = new simple_html_dom();
    $links = array();
    $html = file_get_html('http://jo.opensooq.com/category/%D8%A7%D9%84%D8%B3%D9%8A%D8%A7%D8%B1%D8%A7%D8%AA-%D9%88-%D8%A7%D9%84%D9%85%D8%B1%D9%83%D8%A8%D8%A7%D8%AA/70');

    foreach ($html->find('tr[class="one_post"] td[class="os_new_ads_list_title"] p a')as $a) {
        //echo  $a->href;echo "<br/>";
        $links[] = $a->href;
    }

    foreach ($links as $anchor) {

        $html2 = file_get_html('http://jo.opensooq.com/' . $anchor);

        $one = $html2->find('div[class="os_new_ad_box_lft_details_box"] a');

        $UserModel->name=$one;

        if ($UserModel->save()) {

           echo 'sucsess';                
       } else {

            print_r($UserModel->getErrors());
            echo "<br/>";

       }

    }
}

我有这个输出

    Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name cannot be blank. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )
Array ( [id] => Array ( [0] => ID cannot be blank. ) [name] => Array ( [0] => Name is invalid. ) )

1 个答案:

答案 0 :(得分:0)

检查用户模型中的验证规则。您尝试将不相关的数据发送到DB。尝试将您的表ID更改为自动增量并删除用户模型上的规则

看起来像

public function rules()
{
    return array(
        array('id,username, password', 'required'),  // remove id on this 
        array('password_repeat', 'required', 'on'=>'register'),
        array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'register'),
    );
}

如果规则要求验证者yii通过验证错误,如果您不想验证任何字段,只需删除此列表中的字段

<强>被修改 使用它来保存,在保存时关闭验证规则

$model->save(false);