Zend_Db - 如何使用Zend_Db_Table_Select

时间:2015-04-28 08:39:04

标签: php zend-framework

我被迫在当前的项目中使用Zend-Framework,但我陷入了困境。不幸的是,该框架的文档很糟糕,目前对我没有帮助。解决方案可能非常简单,但我无法自己解决。

我只是想从我的数据库中获取一些东西,我尝试使用此代码实现:

BS_Model_Subshops.php:

class BS_Model_Subshops extends BS_Lib_Model
{
    protected $_name = 'shops';
    protected $_username = '';
    protected $_password = '';
    protected $_primary = 'id';
}

LoginController.php:

public function authAction() {
    $shop= new BS_Model_Subshops();
    $id= filter_var($this->getRequest()->getPost('id'));
    $db_content= $shop->select()->where('id= '.$id);
    Zend_Debug::dump($db_content);

    ... more code ...
}

这给了我以下对象作为结果(警告:巨大的对象传入):

object(Zend_Db_Table_Select)#85 (7) {
["_info":protected] => array(10) {
    ["schema"] => NULL
    ["name"] => string(5) "shops"
    ["cols"] => array(8) {
        [0] => string(2) "id"
      [1] => string(4) "name"
      [2] => string(8) "css_path"
      [3] => string(3) "mmz"
      [4] => string(17) "order_information"
      [5] => string(11) "code_suffix"
    }
    ["primary"] => array(1) {
        [1] => string(2) "id"
    }
    ["metadata"] => array(8) {
        ["id"] => array(14) {
            ["SCHEMA_NAME"] => NULL
        ["TABLE_NAME"] => string(5) "shops"
        ["COLUMN_NAME"] => string(2) "id"
        ["COLUMN_POSITION"] => int(1)
            ["DATA_TYPE"] => string(3) "int"
        ["DEFAULT"] => NULL
        ["NULLABLE"] => bool(false)
            ["LENGTH"] => NULL
        ["SCALE"] => NULL
        ["PRECISION"] => NULL
        ["UNSIGNED"] => bool(true)
            ["PRIMARY"] => bool(true)
            ["PRIMARY_POSITION"] => int(1)
            ["IDENTITY"] => bool(true)
      }
      ["name"] => array(14) {
            ["SCHEMA_NAME"] => NULL
        ["TABLE_NAME"] => string(5) "shops"
        ["COLUMN_NAME"] => string(4) "name"
        ["COLUMN_POSITION"] => int(2)
            ["DATA_TYPE"] => string(7) "varchar"
        ["DEFAULT"] => NULL
        ["NULLABLE"] => bool(false)
            ["LENGTH"] => string(2) "60"
        ["SCALE"] => NULL
        ["PRECISION"] => NULL
        ["UNSIGNED"] => NULL
        ["PRIMARY"] => bool(false)
            ["PRIMARY_POSITION"] => NULL
        ["IDENTITY"] => bool(false)
      }
      ["css_path"] => array(14) {
            ["SCHEMA_NAME"] => NULL
        ["TABLE_NAME"] => string(5) "shops"
        ["COLUMN_NAME"] => string(8) "css_path"
        ["COLUMN_POSITION"] => int(3)
            ["DATA_TYPE"] => string(7) "varchar"
        ["DEFAULT"] => NULL
        ["NULLABLE"] => bool(false)
            ["LENGTH"] => string(3) "100"
        ["SCALE"] => NULL
        ["PRECISION"] => NULL
        ["UNSIGNED"] => NULL
        ["PRIMARY"] => bool(false)
            ["PRIMARY_POSITION"] => NULL
        ["IDENTITY"] => bool(false)
      }
      ["mmz"] => array(14) {
            ["SCHEMA_NAME"] => NULL
        ["TABLE_NAME"] => string(5) "shops"
        ["COLUMN_NAME"] => string(3) "mmz"
        ["COLUMN_POSITION"] => int(4)
            ["DATA_TYPE"] => string(14) "float unsigned"
        ["DEFAULT"] => NULL
        ["NULLABLE"] => bool(false)
            ["LENGTH"] => NULL
        ["SCALE"] => NULL
        ["PRECISION"] => NULL
        ["UNSIGNED"] => bool(true)
            ["PRIMARY"] => bool(false)
            ["PRIMARY_POSITION"] => NULL
        ["IDENTITY"] => bool(false)
      }
      ["order_information"] => array(14) {
            ["SCHEMA_NAME"] => NULL
        ["TABLE_NAME"] => string(5) "shops"
        ["COLUMN_NAME"] => string(17) "order_information"
        ["COLUMN_POSITION"] => int(5)
            ["DATA_TYPE"] => string(4) "text"
        ["DEFAULT"] => NULL
        ["NULLABLE"] => bool(false)
            ["LENGTH"] => NULL
        ["SCALE"] => NULL
        ["PRECISION"] => NULL
        ["UNSIGNED"] => NULL
        ["PRIMARY"] => bool(false)
            ["PRIMARY_POSITION"] => NULL
        ["IDENTITY"] => bool(false)
      }
      ["code_suffix"] => array(14) {
            ["SCHEMA_NAME"] => NULL
        ["TABLE_NAME"] => string(5) "shops"
        ["COLUMN_NAME"] => string(11) "code_suffix"
        ["COLUMN_POSITION"] => int(6)
            ["DATA_TYPE"] => string(7) "varchar"
        ["DEFAULT"] => NULL
        ["NULLABLE"] => bool(false)
            ["LENGTH"] => string(1) "6"
        ["SCALE"] => NULL
        ["PRECISION"] => NULL
        ["UNSIGNED"] => NULL
        ["PRIMARY"] => bool(false)
            ["PRIMARY_POSITION"] => NULL
        ["IDENTITY"] => bool(false)
      }
  ... more unimportant stuff here ...

现在,例如,我只想要名称或css路径。我怎么能得到其中一个?我对Zend并不是很熟悉,尽管之前我和laravel一起工作过,而且我对它的理解存在很大的问题,因为它很复杂,而且没有很好的文档说明。< / p>

如果重要的话,Web服务器使用PHP 5.4和Zend-Framework 1.9。

1 个答案:

答案 0 :(得分:1)

您可以使用 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ScrollView scroll=new ScrollView(this); final LinearLayout layout =new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lparams.setMargins(0, 10, 0, 10); lparams.gravity=Gravity.CENTER; int i; for(i=0;i<15;i++) { //here 'this' is working ****************** ImageView imView = new ImageView(this); imView.setLayoutParams(lparams); Drawable new_image = getResources().getDrawable(R.drawable.rakthasakshi); imView.setBackgroundDrawable(new_image); layout.addView(imView, lparams); } scroll.addView(layout); setContentView(scroll); scroll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP){ //but here says 'this' cannot be applied ############### ImageView imView = new ImageView(this); imView.setLayoutParams(lparams); Drawable new_image = getResources().getDrawable(R.drawable.rakthasakshi); imView.setBackgroundDrawable(new_image); layout.addView(imView, lparams); scroll.addView(layout); setContentView(scroll); return true; } return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 获取名称或css路径

尝试以下代码。

fetchAll

您还可以使用rowset对象的public function authAction() { $shop= new BS_Model_Subshops(); $id= filter_var($this->getRequest()->getPost('id')); $db_content = $shop->select()->where('id= '.$id); $result = $db_content->query()->fetchAll(); foreach($result as $key=>$value){ echo "Name : ". $value->name; echo "</br>"; echo "Css Path : ". $value->css_path; } Zend_Debug::dump($db_content); ... more code ... } 方法将行集检索为数组。

toArray

此致