Heim > php教程 > php手册 > Hauptteil

PHP实体层基础类

WBOY
Freigeben: 2016-06-13 10:17:30
Original
1011 Leute haben es durchsucht

PHP实体层基础类

<span class</span><span  BaseModel
    {
        </span><span private</span> <span $tableName</span><span ;
        </span><span private</span> <span $fields</span>=<span array</span><span ();
        </span><span function</span><span  __construct()
        {
            </span><span $this</span>->tableName=<span rtrim</span>(<span get_class</span>(<span $this</span>),'M'<span );
        }
        </span><span function</span> __get(<span $n</span><span )
        {
            </span><span return</span>(<span $this</span>-><span $n</span><span );
        }
        </span><span function</span> __set(<span $n</span>, <span $value</span><span )
        {
            </span><span $this</span>-><span $n</span> = <span $value</span><span ;
            </span><span $this</span>->fields[<span $n</span>]=<span $value</span><span ;
        }
</span>
        <span function</span><span  Add()
        {
            </span><span $f</span>=''<span ;
            </span><span $f1</span>=''<span ;
            </span><span $arrKeys</span>=<span array_keys</span>(<span $this</span>-><span fields);
            </span><span foreach</span>(<span $arrKeys</span> <span as</span> <span $row</span><span )
            {
                </span><span $f</span>.=<span $row</span>.','<span ;
                </span><span $f1</span>.=':'.<span $row</span>.','<span ;
            }
            </span><span $f</span>=<span rtrim</span>(<span $f</span>,','<span );
            </span><span $f1</span>=<span rtrim</span>(<span $f1</span>,','<span );
            </span><span $sql</span>='insert into '.<span $this</span>->tableName.'('.<span $f</span>.') values('.<span $f1</span>.')'<span ;
            </span><span //</span><span echo $sql;</span>
            <span global</span> <span $dbh</span><span ;
            </span><span return</span> <span $dbh</span>->edit(<span $sql</span>,<span $this</span>-><span fields);
        }
</span>
        <span function</span> Update(<span $sql_where</span>,<span $params</span>=<span array</span><span ())
        {
            </span><span $f</span>=''<span ;
            </span><span $f1</span>=''<span ;
            </span><span $arrKeys</span>=<span array_keys</span>(<span $this</span>-><span fields);        
            </span><span foreach</span>(<span $arrKeys</span> <span as</span> <span $row</span><span )
            {
                </span><span $f</span>.=<span $row</span>.'=:'.<span $row</span>.','<span ;
            }
            </span><span $f</span>=<span rtrim</span>(<span $f</span>,','<span );
            </span><span $sql</span>='update '.<span $this</span>->tableName.' set '.<span $f</span>.' where '.<span $sql_where</span><span ;
            
            </span><span $arrKeys1</span>=<span array_keys</span>(<span $params</span><span );
            </span><span foreach</span>(<span $arrKeys1</span> <span as</span> <span $row</span><span )
            {
                </span><span $this</span>->fields[<span $row</span>]=<span $params</span>[<span $row</span><span ];
            }
            
            </span><span global</span> <span $dbh</span><span ;
            </span><span return</span> <span $dbh</span>->Edit(<span $sql</span>,<span $this</span>-><span fields);
        }
        
        </span><span function</span> Delete(<span $sql_where</span>,<span $params</span>=<span array</span><span ())
        {
            </span><span $sql</span>='delete from '.<span $this</span>->tableName.' where '.<span $sql_where</span><span ;
            </span><span //</span><span echo $sql;</span>
            
            <span global</span> <span $dbh</span><span ;
            </span><span return</span> <span $dbh</span>->Edit(<span $sql</span>,<span $params</span><span );
        }

    }</span>
Nach dem Login kopieren

实体层:

<span class</span> proM <span extends</span><span  BaseModel
{</span>
    <span private</span> <span $proName</span>;
    <span private</span> <span $proDetail</span>;
    <span private</span> <span $proPic</span>;
    <span private</span> <span $proPic1</span>;
    <span private</span> <span $proAttr</span>;
    <span private</span> <span $proTaxis</span>;
    <span private</span> <span $proIsHome</span>;
    <span private</span> <span $proIsTJ</span>;
    <span private</span> <span $proIsCTJ</span>;
    <span private</span> <span $proIsDel</span>;
    <span private</span> <span $proAddTime</span>;
    <span private</span> <span $proisHot</span>;
    <span private</span> <span $proisPorClass</span>;
    <span private</span> <span $proisYN</span>;
    <span private</span> <span $proGL</span>;
    <span private</span> <span $proSeoT</span>;
    <span private</span> <span $proSeoK</span>;
    <span private</span> <span $proSeoD</span>;
    <span private</span> <span $proBH</span><span ;
    </span><span private</span> <span $FK_proClass</span><span ;
    </span><span private</span> <span $proModel</span><span ;
    </span><span private</span> <span $proPrice</span><span ;
    </span><span private</span> <span $proDiscount</span><span ;
    </span><span private</span> <span $proHit</span><span ;
    </span><span private</span> <span $proURL</span><span ;
    </span><span private</span> <span $proisKdWords</span><span ;
    </span><span private</span> <span $proisKdDone</span><span ;
    </span><span private</span> <span $proFL</span><span ;
}</span>
Nach dem Login kopieren

使用:

<span $m</span>=<span new</span><span  proM();
</span><span $m</span>->proName='代码'<span ;
</span><span $m</span>->proDetail='php开发'<span ;
</span><span $m</span>->Update('proID=:proID',<span array</span>(proID=>1));
Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage