search
HomeBackend DevelopmentPHP Tutorialzend framework 数据交互如何使用面向对象

Zend Framework 面向对象 框架

php网站,使用框架是zend framework,在获取数据的时候一般都是写sql语句,用的fetchAll()方法获取数据,像这样:
$db->fetchAll($sql);
获取的是一个array集合,然后做处理,显示在页面上,这其中缺少了一个步骤,就是把数据填充到对象里面,通过对象的方式来赋值到页面,我在网上一直没有找到相关的例子,zend framework使用手册里面也没找到
   所以想请各位给我来个实际一点的例子,纯正的面向对象的,我的项目里面有写model层,但没用到过,在线求具体代码例子

回复讨论(解决方案)

各位,难道没人对这个感兴趣嘛?

"把数据填充到对象里面"这句话我就没看懂...

有点赞同楼上的说法

为什么要把数据赋给对象?再把对象传给页面??

我记得获取数据库适配器之后调用query($sql)方法对数据库进行查询返回的就是一个pdo对象
你可以打印出这个对象 对象里不仅有从数据库中查询到的数据
还包含很多表信息 什么字段 宽度之类的
没什么用嘛..

通常都是把这个对象再调用一下fetchAll()方法 转成数组

你如果实在想向页面传对象
我觉得可以这样做

你自己写好一个类
然后把fetchAll()出来的数组作为参数
传给这个类的构造方法
由构造方法给类中的成员变量赋值

这样也许是"把数据填充到对象里面"了
然后你把这个对象传给页面

但我觉得就算这样 你在view里要想输出对象
还是要对对象进行相关的处理操作




而且用了框架 怎么会没有用到model呢..
难道要用传统的php方法操作数据库么

这样的话,不用那么费力了,自己写个函数,把数组换成类就可以了,干什么一定要找框架里的 ,只要框架准许就可以了,

    大家回答很给力,但是不是我想要的答案,用框架的原因是可以在以后便于维护,扩展,而面向对象是编程的一个主要思路,如果只为实现功能,我就不会提出这个问题了,我需要的是在zend中,是否存在这样的一个库,可以将我查出来的数据转换成类的对象!继续求解。。。。

这么多菜鸟啊,zf2 中 这么多操作方式,Zend\Db\Sql,Zend\Db\TableGateway,Zend\Db\RowGateway,这么都不用啊

在db里写

class Db_Test{  function fetchData(){    $db=new Db();    $res=$db->fetchAll("select * from table");    return $res;  }}

在controller里写
class TestController extends  Zend_Controller_Action{  public function testAction(){    $dbclass=new Db_Test();    $data=$dbclass->fetchData();    $this->view->viewdata=$data;  }}

在模板里写
foreach($this->viewdata as $key=>$value){   echo $value;}

这么多菜鸟啊,zf2 中 这么多操作方式,Zend\Db\Sql,Zend\Db\TableGateway,Zend\Db\RowGateway,这么都不用啊
能说得详细一点吗,或者给出一段代码????

对象数组?
你先说一下为什么要这么做

你再说一下,这样做的应用场景

总不能为类而累吧

其实我就是做网站的,已经做完了,我现在给的问题完全是技术讨论

model层就是放一些业务逻辑
你应该是想把数组放在一个对象的属性上面,
然后在控制器传入一个对象进去。
那么在视图只要循环这个属性就可以获得数组。

我不是不能把数组传到页面,我所谓的对象,指的是数据库里面表,其属性就是表的字段,我无法做到的是把查询出来的数据赋值到表的字段上去?

$db->fetchAll($sql);获取的应该是一个对象集合,用toArray()方法才能转数组,你说的把数据放到对象赋值到页面这个真没明白啊。

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version