CakePHP:在Controller中加载模型

时间:2017-03-16 14:08:40

标签: php cakephp-3.0

我想在我的Controller中加载我的模型。该模型未与数据库中的表关联,因此可能无法遵循CakePHP的ORM。

我目前有以下代码(这是我的模型):

FROM centos:7

# Yum workaround to stalled mirror
RUN sed -i -e 's/enabled=1/enabled=0/g' /etc/yum/pluginconf.d/fastestmirror.conf

RUN rm -f /var/lib/rpm/__*
RUN rpm --rebuilddb -v -v
RUN yum clean all


# see https://bugs.debian.org/775775
# and https://github.com/docker-library/java/issues/19#issuecomment-70546872
ENV CA_CERTIFICATES_JAVA_VERSION 20140324

RUN yum -v install -y \
    wget \
    zip \
    which \
    openssh-client \
    unzip \
    java-1.8.0-openjdk-devel \
    git \
    && yum clean all

#RUN /var/lib/dpkg/info/ca-certificates-java.postinst configure

# Install Tini
ENV TINI_VERSION 0.9.0
ENV TINI_SHA fa23d1e20732501c3bb8eeeca423c89ac80ed452

# Use tini as subreaper in Docker container to adopt zombie processes
RUN curl -fsSL https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini-static -o /bin/tini && chmod +x /bin/tini \
  && echo "$TINI_SHA  /bin/tini" | sha1sum -c -

# SET Jenkins Environment Variables
ENV JENKINS_HOME /var/jenkins_home
ENV JENKINS_SLAVE_AGENT_PORT 50000
ENV JENKINS_VERSION 2.22
ENV JENKINS_SHA 5b89b6967e7af8119c52c7e86223b47665417a22
ENV JENKINS_UC https://updates.jenkins-ci.org
ENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log

# SET Java variables
ENV JAVA_HOME /usr/lib/jvm/java/jre
ENV PATH /usr/lib/jvm/java/bin:$PATH

# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN useradd -d "$JENKINS_HOME" -u 1000 -m -s /bin/bash jenkins

#Not working. Folder not yet mounted?
#RUN DOCKER_GID=$(stat -c '%g' /var/run/docker.sock) && \

#Using gid from host
RUN groupadd -for -g 50 docker && \
    usermod -aG docker jenkins

# Jenkins home directory is a volume, so configuration and build history
# can be persisted and survive image upgrades
VOLUME /var/jenkins_home

# `/usr/share/jenkins/ref/` contains all reference configuration we want
# to set on a fresh new installation. Use it to bundle additional plugins
# or config file with your custom jenkins Docker image.
RUN mkdir -p /usr/share/jenkins/ref/init.groovy.d

# Install Jenkins
RUN curl -fL http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war -o /usr/share/jenkins/jenkins.war \
  && echo "$JENKINS_SHA /usr/share/jenkins/jenkins.war" | sha1sum -c -

ENV JAVA_OPTS="-Xmx8192m"
ENV JENKINS_OPTS="--logfile=/var/log/jenkins/jenkins.log  --webroot=/var/cache/jenkins/war"

# Prep Jenkins Directories
RUN chown -R jenkins "$JENKINS_HOME" /usr/share/jenkins/ref
RUN mkdir /var/log/jenkins
RUN mkdir /var/cache/jenkins
RUN chown -R jenkins:jenkins /var/log/jenkins
RUN chown -R jenkins:jenkins /var/cache/jenkins

# Expose Ports for web and slave agents
EXPOSE 8080
EXPOSE 50000

# Copy in local config files
COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy
COPY jenkins.sh /usr/local/bin/jenkins.sh
COPY plugins.sh /usr/local/bin/plugins.sh
RUN chmod +x /usr/local/bin/plugins.sh
RUN chmod +x /usr/local/bin/jenkins.sh


# Install default plugins
COPY plugins.txt /tmp/plugins.txt
RUN /usr/local/bin/plugins.sh /tmp/plugins.txt


# Add ssh key
RUN eval "$(ssh-agent -s)"
RUN mkdir /usr/share/jenkins/ref/.ssh && \
    chmod 700 /usr/share/jenkins/ref/.ssh && \
    ssh-keyscan github.com > /usr/share/jenkins/ref/.ssh/known_hosts

COPY id_rsa /usr/share/jenkins/ref/.ssh/id_rsa
COPY id_rsa /usr/share/jenkins/ref/.ssh/id_rsa.pub
COPY hudson.tasks.Maven.xml /usr/share/jenkins/ref/hudson.tasks.Maven.xml

RUN chown -R jenkins:jenkins /usr/share/jenkins/ref && \
    chmod 600 /usr/share/jenkins/ref/.ssh/id_rsa && \
    chmod 600 /usr/share/jenkins/ref/.ssh/id_rsa.pub && \
    chmod 600 /usr/share/jenkins/ref/hudson.tasks.Maven.xml

COPY id_rsa /root/.ssh/id_rsa
COPY id_rsa /root/.ssh/id_rsa.pub

# ssh keys for root. To use root as the user
RUN chmod 600 /root/.ssh/id_rsa && \
    chmod 600 /root/.ssh/id_rsa.pub && \
    ssh-keyscan github.com > /root/.ssh/known_hosts

# Switch to the jenkins user
USER jenkins

# Tini as the entry point to manage zombie processes
ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]

我通过以下代码(在控制器中)调用模型:

<?php
namespace App\Model\Json;

use Cake\Filesystem\File;

 class Processes
 {

        public static function getData()
        {

            $file = new File('process_data.json');
            $json = $file->read(true, 'r');

            $jsonstd = json_decode($json);

            // remove STD classes
            $json2array = json_decode(json_encode($jsonstd), true);

            $cpu = array();

            foreach ($json2array as $key => $row)
            {
                $cpu[$key] = $row['cpu_usage_precent'];
            }
            array_multisort($cpu, SORT_DESC, $json2array);
            // return data
            return $json2array;
        }
}

我无法以某种方式在我的控制器中调用它。我一直收到以下错误:

  

应用程序中的某些Table对象是由   实例化&#34; Cake \ ORM \ Table&#34;而不是任何其他特定的子类。

     

请尝试更正以下表别名的问题:

     

流程

1 个答案:

答案 0 :(得分:0)

以下示例是$devices = array('uniqueId' => '1234','status' => 'online','lastUpdate' => time(),'phone' => '1234','model' => 'test','contact' => 'admin' $new_keys = array('IMEI','Status','Last Update','Phone','Model','Contact'); for ($i = 0; $i < count($devices) - 1; $i++) { array_replace($devices[$i], $new_keys[$i]); }

中如何访问Model CakePHP ORM

在模型中: CakePHP 3.x

  

在档案Processes

/path_to/src/Model/Table/Processes.php

在控制器中: namespace App\Model\Table; #Define the namespace use Cake\Filesystem\File; class Processes{ public static function getData(){ /*Your codes here*/ } }

  

在档案Bookmarks

/path_to/src/Controller/BookmarksController.php
  

以下是Auto loading with PSR-4

的详细信息